Skip to content

Commit

Permalink
Allow dev_dependencies and build_dependencies as alternatives to dev-…
Browse files Browse the repository at this point in the history
…dependencies and build-dependencies
  • Loading branch information
hdoordt committed Oct 30, 2024
1 parent da70f52 commit 36a50dd
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! manifest_deps_toml {
($first:literal, $second:literal, $manifest_toml:expr) => {
if let Some(i) = $manifest_toml
.get_mut($first)
.and_then(|d| d.as_table_mut())
{
i
} else if let Some(i) = $manifest_toml
.get_mut($second)
.and_then(|d| d.as_table_mut())
{
i
} else {
anyhow::bail!(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 =
manifest_deps_toml!("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 =
manifest_deps_toml!("build-dependencies", "build_dependencies", manifest_toml);

inherit_deps(
deps,
deps_toml,
Expand Down

0 comments on commit 36a50dd

Please sign in to comment.