Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow dev_dependencies and build_dependencies as alternatives to dev-… #31

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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