diff options
author | Zac Pullar-Strecker <[email protected]> | 2020-08-24 10:19:53 +0100 |
---|---|---|
committer | Zac Pullar-Strecker <[email protected]> | 2020-08-24 10:20:13 +0100 |
commit | 7bbca7a1b3f9293d2f5cc5745199bc5f8396f2f0 (patch) | |
tree | bdb47765991cb973b2cd5481a088fac636bd326c /crates/cfg/src/lib.rs | |
parent | ca464650eeaca6195891199a93f4f76cf3e7e697 (diff) | |
parent | e65d48d1fb3d4d91d9dc1148a7a836ff5c9a3c87 (diff) |
Merge remote-tracking branch 'upstream/master' into 503-hover-doc-links
Diffstat (limited to 'crates/cfg/src/lib.rs')
-rw-r--r-- | crates/cfg/src/lib.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/crates/cfg/src/lib.rs b/crates/cfg/src/lib.rs new file mode 100644 index 000000000..a9d50e698 --- /dev/null +++ b/crates/cfg/src/lib.rs | |||
@@ -0,0 +1,51 @@ | |||
1 | //! cfg defines conditional compiling options, `cfg` attibute parser and evaluator | ||
2 | |||
3 | mod cfg_expr; | ||
4 | |||
5 | use rustc_hash::FxHashSet; | ||
6 | use tt::SmolStr; | ||
7 | |||
8 | pub use cfg_expr::CfgExpr; | ||
9 | |||
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 | ||
12 | /// key-value options like `target_arch="x86"`. | ||
13 | /// | ||
14 | /// Note that for key-value options, one key can have multiple values (but not none). | ||
15 | /// `feature` is an example. We have both `feature="foo"` and `feature="bar"` if features | ||
16 | /// `foo` and `bar` are both enabled. And here, we store key-value options as a set of tuple | ||
17 | /// of key and value in `key_values`. | ||
18 | /// | ||
19 | /// See: https://doc.rust-lang.org/reference/conditional-compilation.html#set-configuration-options | ||
20 | #[derive(Debug, Clone, PartialEq, Eq, Default)] | ||
21 | pub struct CfgOptions { | ||
22 | atoms: FxHashSet<SmolStr>, | ||
23 | key_values: FxHashSet<(SmolStr, SmolStr)>, | ||
24 | } | ||
25 | |||
26 | impl CfgOptions { | ||
27 | pub fn check(&self, cfg: &CfgExpr) -> Option<bool> { | ||
28 | cfg.fold(&|key, value| match value { | ||
29 | None => self.atoms.contains(key), | ||
30 | Some(value) => self.key_values.contains(&(key.clone(), value.clone())), | ||
31 | }) | ||
32 | } | ||
33 | |||
34 | pub fn insert_atom(&mut self, key: SmolStr) { | ||
35 | self.atoms.insert(key); | ||
36 | } | ||
37 | |||
38 | pub fn insert_key_value(&mut self, key: SmolStr, value: SmolStr) { | ||
39 | self.key_values.insert((key, value)); | ||
40 | } | ||
41 | |||
42 | pub fn append(&mut self, other: &CfgOptions) { | ||
43 | for atom in &other.atoms { | ||
44 | self.atoms.insert(atom.clone()); | ||
45 | } | ||
46 | |||
47 | for (key, value) in &other.key_values { | ||
48 | self.key_values.insert((key.clone(), value.clone())); | ||
49 | } | ||
50 | } | ||
51 | } | ||