diff options
Diffstat (limited to 'crates/cfg/src/lib.rs')
-rw-r--r-- | crates/cfg/src/lib.rs | 22 |
1 files changed, 7 insertions, 15 deletions
diff --git a/crates/cfg/src/lib.rs b/crates/cfg/src/lib.rs index a9d50e698..35f540ac3 100644 --- a/crates/cfg/src/lib.rs +++ b/crates/cfg/src/lib.rs | |||
@@ -5,7 +5,7 @@ mod cfg_expr; | |||
5 | use rustc_hash::FxHashSet; | 5 | use rustc_hash::FxHashSet; |
6 | use tt::SmolStr; | 6 | use tt::SmolStr; |
7 | 7 | ||
8 | pub use cfg_expr::CfgExpr; | 8 | pub use cfg_expr::{CfgAtom, CfgExpr}; |
9 | 9 | ||
10 | /// Configuration options used for conditional compilition on items with `cfg` attributes. | 10 | /// Configuration options used for conditional compilition on items with `cfg` attributes. |
11 | /// We have two kind of options in different namespaces: atomic options like `unix`, and | 11 | /// We have two kind of options in different namespaces: atomic options like `unix`, and |
@@ -19,33 +19,25 @@ pub use cfg_expr::CfgExpr; | |||
19 | /// See: https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options | 19 | /// See: https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options |
20 | #[derive(Debug, Clone, PartialEq, Eq, Default)] | 20 | #[derive(Debug, Clone, PartialEq, Eq, Default)] |
21 | pub struct CfgOptions { | 21 | pub struct CfgOptions { |
22 | atoms: FxHashSet<SmolStr>, | 22 | enabled: FxHashSet<CfgAtom>, |
23 | key_values: FxHashSet<(SmolStr, SmolStr)>, | ||
24 | } | 23 | } |
25 | 24 | ||
26 | impl CfgOptions { | 25 | impl CfgOptions { |
27 | pub fn check(&self, cfg: &CfgExpr) -> Option<bool> { | 26 | pub fn check(&self, cfg: &CfgExpr) -> Option<bool> { |
28 | cfg.fold(&|key, value| match value { | 27 | cfg.fold(&|atom| self.enabled.contains(atom)) |
29 | None => self.atoms.contains(key), | ||
30 | Some(value) => self.key_values.contains(&(key.clone(), value.clone())), | ||
31 | }) | ||
32 | } | 28 | } |
33 | 29 | ||
34 | pub fn insert_atom(&mut self, key: SmolStr) { | 30 | pub fn insert_atom(&mut self, key: SmolStr) { |
35 | self.atoms.insert(key); | 31 | self.enabled.insert(CfgAtom::Flag(key)); |
36 | } | 32 | } |
37 | 33 | ||
38 | pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) { | 34 | pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) { |
39 | self.key_values.insert((key, value)); | 35 | self.enabled.insert(CfgAtom::KeyValue { key, value }); |
40 | } | 36 | } |
41 | 37 | ||
42 | pub fn append(&mut self, other: &CfgOptions) { | 38 | pub fn append(&mut self, other: &CfgOptions) { |
43 | for atom in &other.atoms { | 39 | for atom in &other.enabled { |
44 | self.atoms.insert(atom.clone()); | 40 | self.enabled.insert(atom.clone()); |
45 | } | ||
46 | |||
47 | for (key, value) in &other.key_values { | ||
48 | self.key_values.insert((key.clone(), value.clone())); | ||
49 | } | 41 | } |
50 | } | 42 | } |
51 | } | 43 | } |