Skip to content

Commit

Permalink
Move features to feature2 that depend on features that use new fe…
Browse files Browse the repository at this point in the history
…ature syntax
  • Loading branch information
Turbo87 committed Oct 4, 2024
1 parent 22cbdb8 commit 187b3c9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 11 deletions.
41 changes: 34 additions & 7 deletions src/models/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,38 @@ pub type FeaturesMap = BTreeMap<String, Vec<String>>;
///
/// See <https://rust-lang.github.io/rfcs/3143-cargo-weak-namespaced-features.html>.
pub fn split_features(features: FeaturesMap) -> (FeaturesMap, FeaturesMap) {
features.into_iter().partition(|(_k, vals)| {
!vals
.iter()
.any(|v| v.starts_with("dep:") || v.contains("?/"))
})
const ITERATION_LIMIT: usize = 100;

// First, we partition the features into two groups: those that use the new
// features syntax (`features2`) and those that don't (`features`).
let (mut features, mut features2) =
features
.into_iter()
.partition::<FeaturesMap, _>(|(_k, vals)| {
!vals
.iter()
.any(|v| v.starts_with("dep:") || v.contains("?/"))
});

// Then, we recursively move features from `features` to `features2` if they
// depend on features in `features2`.
for _ in 0..ITERATION_LIMIT {
let split = features
.into_iter()
.partition::<FeaturesMap, _>(|(_k, vals)| {
!vals.iter().any(|v| features2.contains_key(v))
});

features = split.0;

if !split.1.is_empty() {
features2.extend(split.1);
} else {
break;
}
}

(features, features2)
}

#[cfg(test)]
Expand Down Expand Up @@ -93,8 +120,8 @@ mod tests {

let (features, features2) = split_features(features);

assert_compact_debug_snapshot!(features, @r#"{"feature1": ["feature2"], "feature2": ["feature3"]}"#);
assert_compact_debug_snapshot!(features2, @r#"{"feature3": ["dep:foo"]}"#);
assert_compact_debug_snapshot!(features, @"{}");
assert_compact_debug_snapshot!(features2, @r#"{"feature1": ["feature2"], "feature2": ["feature3"], "feature3": ["dep:foo"]}"#);
}

#[test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,6 @@ expression: split_features(features)
"unicode": [
"clap_builder/unicode",
],
"unstable-doc": [
"clap_builder/unstable-doc",
"derive",
],
"unstable-ext": [
"clap_builder/unstable-ext",
],
Expand All @@ -68,6 +64,10 @@ expression: split_features(features)
"derive": [
"dep:clap_derive",
],
"unstable-doc": [
"clap_builder/unstable-doc",
"derive",
],
"unstable-v5": [
"clap_builder/unstable-v5",
"clap_derive?/unstable-v5",
Expand Down

0 comments on commit 187b3c9

Please sign in to comment.