Skip to content

Commit

Permalink
Merge pull request #31 from mainmatter/dependency-table-underscore-alias
Browse files Browse the repository at this point in the history
Allow dev_dependencies and build_dependencies as alternatives to dev-…
  • Loading branch information
hdoordt authored Nov 1, 2024
2 parents da70f52 + c3def4b commit b598b71
Showing 1 changed file with 33 additions and 6 deletions.
39 changes: 33 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ fn rewrite_dep_path_as_relative<P: AsRef<std::path::Path>>(dep: &mut Dependency,
}
}

// Gets the first entry out of the document as a table if it exists,
// or gets the second one if it doesn't. If that doesn't exist
// either, then it returns an error.
// Borrowing rules make it hard to do this in a function,
// so here we are.
macro_rules! get_either_table_mut {
($first:literal, $second:literal, $manifest_toml:expr) => {
if let Some(i) = $manifest_toml
.get_mut($first)
.and_then(|d| d.as_table_mut())
{
Ok(i)
} else if let Some(i) = $manifest_toml
.get_mut($second)
.and_then(|d| d.as_table_mut())
{
Ok(i)
} else {
Err(anyhow::anyhow!(concat!(
"Failed to find `[",
$first,
"]` table in root manifest."
)))
}
};
}

pub fn auto_inherit(conf: &AutoInheritConf) -> Result<(), anyhow::Error> {
let metadata = guppy::MetadataCommand::new().exec().context(
"Failed to execute `cargo metadata`. Was the command invoked inside a Rust project?",
Expand Down Expand Up @@ -187,9 +214,9 @@ pub fn auto_inherit(conf: &AutoInheritConf) -> Result<(), anyhow::Error> {
);
}
if let Some(deps) = &manifest.dev_dependencies {
let deps_toml = manifest_toml["dev-dependencies"]
.as_table_mut()
.expect("Failed to find `[dev-dependencies]` table in root manifest.");
let deps_toml =
get_either_table_mut!("dev-dependencies", "dev_dependencies", manifest_toml)?;

inherit_deps(
deps,
deps_toml,
Expand All @@ -199,9 +226,9 @@ pub fn auto_inherit(conf: &AutoInheritConf) -> Result<(), anyhow::Error> {
);
}
if let Some(deps) = &manifest.build_dependencies {
let deps_toml = manifest_toml["build-dependencies"]
.as_table_mut()
.expect("Failed to find `[build-dependencies]` table in root manifest.");
let deps_toml =
get_either_table_mut!("build-dependencies", "build_dependencies", manifest_toml)?;

inherit_deps(
deps,
deps_toml,
Expand Down

0 comments on commit b598b71

Please sign in to comment.