diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-08-13 11:10:03 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-08-13 11:10:03 +0100 |
commit | de1d93455f85747410efb69c28e0c1379e8e328a (patch) | |
tree | 20eb4df06fc99236a30a98bba8e7ea3200b87042 /crates/project_model/src/cfg_flag.rs | |
parent | 8870a5ea138bb4ba048140d90d728721c4b4ad4b (diff) | |
parent | eac24d52e672c0a9c118e8969bf1b839c3e7f1f3 (diff) |
Merge #5744
5744: Rename ra_project_model -> project_model r=matklad a=pksunkara
Co-authored-by: Pavan Kumar Sunkara <[email protected]>
Diffstat (limited to 'crates/project_model/src/cfg_flag.rs')
-rw-r--r-- | crates/project_model/src/cfg_flag.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/crates/project_model/src/cfg_flag.rs b/crates/project_model/src/cfg_flag.rs new file mode 100644 index 000000000..e92962cf6 --- /dev/null +++ b/crates/project_model/src/cfg_flag.rs | |||
@@ -0,0 +1,51 @@ | |||
1 | //! Parsing of CfgFlags as command line arguments, as in | ||
2 | //! | ||
3 | //! rustc main.rs --cfg foo --cfg 'feature="bar"' | ||
4 | use std::str::FromStr; | ||
5 | |||
6 | use cfg::CfgOptions; | ||
7 | use stdx::split_once; | ||
8 | |||
9 | #[derive(Clone, Eq, PartialEq, Debug)] | ||
10 | pub enum CfgFlag { | ||
11 | Atom(String), | ||
12 | KeyValue { key: String, value: String }, | ||
13 | } | ||
14 | |||
15 | impl FromStr for CfgFlag { | ||
16 | type Err = String; | ||
17 | fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
18 | let res = match split_once(s, '=') { | ||
19 | Some((key, value)) => { | ||
20 | if !(value.starts_with('"') && value.ends_with('"')) { | ||
21 | return Err(format!("Invalid cfg ({:?}), value should be in quotes", s)); | ||
22 | } | ||
23 | let key = key.to_string(); | ||
24 | let value = value[1..value.len() - 1].to_string(); | ||
25 | CfgFlag::KeyValue { key, value } | ||
26 | } | ||
27 | None => CfgFlag::Atom(s.into()), | ||
28 | }; | ||
29 | Ok(res) | ||
30 | } | ||
31 | } | ||
32 | |||
33 | impl<'de> serde::Deserialize<'de> for CfgFlag { | ||
34 | fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
35 | where | ||
36 | D: serde::Deserializer<'de>, | ||
37 | { | ||
38 | String::deserialize(deserializer)?.parse().map_err(serde::de::Error::custom) | ||
39 | } | ||
40 | } | ||
41 | |||
42 | impl Extend<CfgFlag> for CfgOptions { | ||
43 | fn extend<T: IntoIterator<Item = CfgFlag>>(&mut self, iter: T) { | ||
44 | for cfg_flag in iter { | ||
45 | match cfg_flag { | ||
46 | CfgFlag::Atom(it) => self.insert_atom(it.into()), | ||
47 | CfgFlag::KeyValue { key, value } => self.insert_key_value(key.into(), value.into()), | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | } | ||