aboutsummaryrefslogtreecommitdiff
path: root/development/tools/cargo-workspace2/src/cargo/deps.rs
blob: d40170ccd841c0f3ee263f907795cf6eae4553fa (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
use super::v_to_s;
use toml_edit::InlineTable;

/// An intra-workspace dependency
///
/// In a Cargo.toml file these are expressed as paths, and sometimes
/// also as versions.
///
/// ```toml
/// [dependencies]
/// my-other-crate = { version = "0.1.0", path = "../other-crate" }
/// ```
#[derive(Debug, Clone)]
pub struct Dependency {
    pub name: String,
    pub alias: Option<String>,
    pub version: Option<String>,
    pub path: Option<String>,
}

impl Dependency {
    pub(crate) fn parse(n: String, t: &InlineTable) -> Option<Self> {
        let v = t.get("version").map(|s| v_to_s(s));
        let p = t.get("path").map(|s| v_to_s(s));

        // If a `package` key is present, set it as the name, and set
        // the `n` as the alias.  When we look for keys later, the
        // alias has precedence over the actual name, but this way
        // `name` is always the actual crate name which is important
        // for dependency resolution.
        let (alias, name) = match t
            .get("package")
            .map(|s| v_to_s(s).replace("\"", "").trim().to_string())
        {
            Some(alias) => (Some(n), alias),
            None => (None, n),
        };

        match (v, p) {
            (version @ Some(_), path @ Some(_)) => Some(Self {
                name,
                alias,
                version,
                path,
            }),
            (version @ Some(_), None) => Some(Self {
                name,
                alias,
                version,
                path: None,
            }),
            (None, path @ Some(_)) => Some(Self {
                name,
                alias,
                version: None,
                path,
            }),
            (None, None) => None,
        }
    }

    /// Check if the dependency has a provided version
    pub fn has_version(&self) -> bool {
        self.version.is_some()
    }

    /// Check if the dependency has a provided path
    pub fn has_path(&self) -> bool {
        self.path.is_some()
    }

    pub fn alias(&self) -> Option<String> {
        self.alias.clone()
    }
}