Created
June 1, 2023 16:39
-
-
Save divi255/8575aa22df0825291933fd03194421f6 to your computer and use it in GitHub Desktop.
Use cargo metadata to deny build if some dependency crate feature has been enabled
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use cargo_metadata::{CargoOpt, MetadataCommand}; | |
use std::path::Path; | |
fn main() { | |
let mut manifest = Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()).to_owned(); | |
manifest.push("Cargo.toml"); | |
let metadata = MetadataCommand::new() | |
.manifest_path(manifest) | |
.features(CargoOpt::AllFeatures) | |
.exec() | |
.unwrap(); | |
if let Some(resolve) = metadata.resolve { | |
for node in &resolve.nodes { | |
if node.id.repr.starts_with("serde_json ") { | |
if node.features.iter().any(|v| v == "arbitrary_precision") { | |
let deps = resolve | |
.nodes | |
.iter() | |
.filter(|d| d.dependencies.contains(&node.id)) | |
.map(|d| d.id.repr.as_str()) | |
.collect::<Vec<&str>>(); | |
panic!( | |
"serde_json arbitrary_precision MUST be off. Dependents: \n\n{}\n\n", | |
deps.join("\n") | |
); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment