aboutsummaryrefslogtreecommitdiff
path: root/crates/project_model/src/cfg_flag.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-08-13 11:10:03 +0100
committerGitHub <[email protected]>2020-08-13 11:10:03 +0100
commitde1d93455f85747410efb69c28e0c1379e8e328a (patch)
tree20eb4df06fc99236a30a98bba8e7ea3200b87042 /crates/project_model/src/cfg_flag.rs
parent8870a5ea138bb4ba048140d90d728721c4b4ad4b (diff)
parenteac24d52e672c0a9c118e8969bf1b839c3e7f1f3 (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.rs51
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"'
4use std::str::FromStr;
5
6use cfg::CfgOptions;
7use stdx::split_once;
8
9#[derive(Clone, Eq, PartialEq, Debug)]
10pub enum CfgFlag {
11 Atom(String),
12 KeyValue { key: String, value: String },
13}
14
15impl 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
33impl<'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
42impl 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}