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 1, 2024
1 parent 7f6f0a0 commit 67a52fc
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
39 changes: 32 additions & 7 deletions src/models/feature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,36 @@ 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("?/"))
})
// 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`.
loop {
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 +118,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 @@ -14,6 +14,10 @@ expression: features2
"derive": [
"dep:clap_derive",
],
"unstable-doc": [
"clap_builder/unstable-doc",
"derive",
],
"unstable-v5": [
"clap_builder/unstable-v5",
"clap_derive?/unstable-v5",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,6 @@ expression: features
"unicode": [
"clap_builder/unicode",
],
"unstable-doc": [
"clap_builder/unstable-doc",
"derive",
],
"unstable-ext": [
"clap_builder/unstable-ext",
],
Expand Down

0 comments on commit 67a52fc

Please sign in to comment.