1 //! Parsing of CfgFlags as command line arguments, as in
3 //! rustc main.rs --cfg foo --cfg 'feature="bar"'
9 #[derive(Clone, Eq, PartialEq, Debug)]
12 KeyValue { key: String, value: String },
15 impl FromStr for CfgFlag {
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));
23 let key = key.to_string();
24 let value = value[1..value.len() - 1].to_string();
25 CfgFlag::KeyValue { key, value }
27 None => CfgFlag::Atom(s.into()),
33 impl<'de> serde::Deserialize<'de> for CfgFlag {
34 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
36 D: serde::Deserializer<'de>,
38 String::deserialize(deserializer)?.parse().map_err(serde::de::Error::custom)
42 impl Extend<CfgFlag> for CfgOptions {
43 fn extend<T: IntoIterator<Item = CfgFlag>>(&mut self, iter: T) {
44 for cfg_flag in iter {
46 CfgFlag::Atom(it) => self.insert_atom(it.into()),
47 CfgFlag::KeyValue { key, value } => self.insert_key_value(key.into(), value.into()),