aboutsummaryrefslogtreecommitdiff
path: root/crates/rust-analyzer/src/cargo_target_spec.rs
blob: f87bdcec5c4a371df73e7105397c3fde61bc7c61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
//! See `CargoTargetSpec`

use ra_ide::{FileId, RunnableKind, TestId};
use ra_project_model::{self, ProjectWorkspace, TargetKind};

use crate::{world::WorldSnapshot, Result};

/// Abstract representation of Cargo target.
///
/// We use it to cook up the set of cli args we need to pass to Cargo to
/// build/test/run the target.
pub(crate) struct CargoTargetSpec {
    pub(crate) package: String,
    pub(crate) target: String,
    pub(crate) target_kind: TargetKind,
}

impl CargoTargetSpec {
    pub(crate) fn runnable_args(
        spec: Option<CargoTargetSpec>,
        kind: &RunnableKind,
    ) -> Result<(Vec<String>, Vec<String>)> {
        let mut args = Vec::new();
        let mut extra_args = Vec::new();
        match kind {
            RunnableKind::Test { test_id } => {
                args.push("test".to_string());
                if let Some(spec) = spec {
                    spec.push_to(&mut args);
                }
                extra_args.push(test_id.to_string());
                if let TestId::Path(_) = test_id {
                    extra_args.push("--exact".to_string());
                }
                extra_args.push("--nocapture".to_string());
            }
            RunnableKind::TestMod { path } => {
                args.push("test".to_string());
                if let Some(spec) = spec {
                    spec.push_to(&mut args);
                }
                extra_args.push(path.to_string());
                extra_args.push("--nocapture".to_string());
            }
            RunnableKind::Bench { test_id } => {
                args.push("bench".to_string());
                if let Some(spec) = spec {
                    spec.push_to(&mut args);
                }
                extra_args.push(test_id.to_string());
                if let TestId::Path(_) = test_id {
                    extra_args.push("--exact".to_string());
                }
                extra_args.push("--nocapture".to_string());
            }
            RunnableKind::Bin => {
                args.push("run".to_string());
                if let Some(spec) = spec {
                    spec.push_to(&mut args);
                }
            }
        }
        Ok((args, extra_args))
    }

    pub(crate) fn for_file(
        world: &WorldSnapshot,
        file_id: FileId,
    ) -> Result<Option<CargoTargetSpec>> {
        let &crate_id = match world.analysis().crate_for(file_id)?.first() {
            Some(crate_id) => crate_id,
            None => return Ok(None),
        };
        let file_id = world.analysis().crate_root(crate_id)?;
        let path = world.file_id_to_path(file_id);
        let res = world.workspaces.iter().find_map(|ws| match ws {
            ProjectWorkspace::Cargo { cargo, .. } => {
                let tgt = cargo.target_by_root(&path)?;
                Some(CargoTargetSpec {
                    package: cargo[cargo[tgt].package].name.clone(),
                    target: cargo[tgt].name.clone(),
                    target_kind: cargo[tgt].kind,
                })
            }
            ProjectWorkspace::Json { .. } => None,
        });
        Ok(res)
    }

    pub(crate) fn push_to(self, buf: &mut Vec<String>) {
        buf.push("--package".to_string());
        buf.push(self.package);
        match self.target_kind {
            TargetKind::Bin => {
                buf.push("--bin".to_string());
                buf.push(self.target);
            }
            TargetKind::Test => {
                buf.push("--test".to_string());
                buf.push(self.target);
            }
            TargetKind::Bench => {
                buf.push("--bench".to_string());
                buf.push(self.target);
            }
            TargetKind::Example => {
                buf.push("--example".to_string());
                buf.push(self.target);
            }
            TargetKind::Lib => {
                buf.push("--lib".to_string());
            }
            TargetKind::Other => (),
        }
    }
}