diff options
Diffstat (limited to 'development/tools/cargo-workspace2/src/cargo/deps.rs')
-rw-r--r-- | development/tools/cargo-workspace2/src/cargo/deps.rs | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/development/tools/cargo-workspace2/src/cargo/deps.rs b/development/tools/cargo-workspace2/src/cargo/deps.rs new file mode 100644 index 000000000000..d40170ccd841 --- /dev/null +++ b/development/tools/cargo-workspace2/src/cargo/deps.rs @@ -0,0 +1,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() + } +} |