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, pub version: Option, pub path: Option, } impl Dependency { pub(crate) fn parse(n: String, t: &InlineTable) -> Option { 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 { self.alias.clone() } }