My personal project and infrastructure archive
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
nomicon/development/tools/cargo-workspace2/src/cargo/deps.rs

75 lines
2.1 KiB

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()
}
}