diff options
Diffstat (limited to 'crates/cfg')
-rw-r--r-- | crates/cfg/src/lib.rs | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/crates/cfg/src/lib.rs b/crates/cfg/src/lib.rs index 03b8dd767..9a4baa636 100644 --- a/crates/cfg/src/lib.rs +++ b/crates/cfg/src/lib.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | //! cfg defines conditional compiling options, `cfg` attibute parser and evaluator | 1 | //! cfg defines conditional compiling options, `cfg` attribute parser and evaluator |
2 | 2 | ||
3 | mod cfg_expr; | 3 | mod cfg_expr; |
4 | mod dnf; | 4 | mod dnf; |
@@ -50,8 +50,29 @@ impl CfgOptions { | |||
50 | self.enabled.remove(&atom); | 50 | self.enabled.remove(&atom); |
51 | } | 51 | } |
52 | } | 52 | } |
53 | |||
54 | pub fn get_cfg_keys(&self) -> Vec<&SmolStr> { | ||
55 | self.enabled | ||
56 | .iter() | ||
57 | .map(|x| match x { | ||
58 | CfgAtom::Flag(key) => key, | ||
59 | CfgAtom::KeyValue { key, .. } => key, | ||
60 | }) | ||
61 | .collect() | ||
62 | } | ||
63 | |||
64 | pub fn get_cfg_values(&self, cfg_key: &str) -> Vec<&SmolStr> { | ||
65 | self.enabled | ||
66 | .iter() | ||
67 | .filter_map(|x| match x { | ||
68 | CfgAtom::KeyValue { key, value } if cfg_key == key => Some(value), | ||
69 | _ => None, | ||
70 | }) | ||
71 | .collect() | ||
72 | } | ||
53 | } | 73 | } |
54 | 74 | ||
75 | #[derive(Clone, Debug, PartialEq, Eq)] | ||
55 | pub struct CfgDiff { | 76 | pub struct CfgDiff { |
56 | // Invariants: No duplicates, no atom that's both in `enable` and `disable`. | 77 | // Invariants: No duplicates, no atom that's both in `enable` and `disable`. |
57 | enable: Vec<CfgAtom>, | 78 | enable: Vec<CfgAtom>, |
@@ -59,6 +80,20 @@ pub struct CfgDiff { | |||
59 | } | 80 | } |
60 | 81 | ||
61 | impl CfgDiff { | 82 | impl CfgDiff { |
83 | /// Create a new CfgDiff. Will return None if the same item appears more than once in the set | ||
84 | /// of both. | ||
85 | pub fn new(enable: Vec<CfgAtom>, disable: Vec<CfgAtom>) -> Option<CfgDiff> { | ||
86 | let mut occupied = FxHashSet::default(); | ||
87 | for item in enable.iter().chain(disable.iter()) { | ||
88 | if !occupied.insert(item) { | ||
89 | // was present | ||
90 | return None; | ||
91 | } | ||
92 | } | ||
93 | |||
94 | Some(CfgDiff { enable, disable }) | ||
95 | } | ||
96 | |||
62 | /// Returns the total number of atoms changed by this diff. | 97 | /// Returns the total number of atoms changed by this diff. |
63 | pub fn len(&self) -> usize { | 98 | pub fn len(&self) -> usize { |
64 | self.enable.len() + self.disable.len() | 99 | self.enable.len() + self.disable.len() |