Skip to content

Commit

Permalink
Implement enum config options deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielMajeri committed Jul 6, 2020
1 parent 4c7be8d commit 65fc4ce
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/cargo/util/config/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,11 +195,31 @@ impl<'de, 'config> de::Deserializer<'de> for Deserializer<'config> {
}
}

fn deserialize_enum<V>(
self,
_name: &'static str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: de::Visitor<'de>,
{
let value = self
.config
.get_string_priv(&self.key)?
.ok_or_else(|| ConfigError::missing(&self.key))?;

let Value { val, definition } = value;
visitor
.visit_enum(val.into_deserializer())
.map_err(|e: ConfigError| e.with_key_context(&self.key, definition))
}

// These aren't really supported, yet.
serde::forward_to_deserialize_any! {
f32 f64 char str bytes
byte_buf unit unit_struct
enum identifier ignored_any
identifier ignored_any
}
}

Expand Down
23 changes: 23 additions & 0 deletions tests/testsuite/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,3 +1276,26 @@ strip = 'debuginfo'
let strip = p.strip.unwrap();
assert_eq!(strip, Strip::DebugInfo);
}

#[cargo_test]
fn parse_enum_fail() {
write_config(
"\
[profile.release]
strip = 'invalid'
",
);

let config = new_config();

assert_error(
config
.get::<toml::TomlProfile>("profile.release")
.unwrap_err(),
"\
error in [..]/.cargo/config: could not load config key `profile.release.strip`
Caused by:
unknown variant `invalid`, expected one of `debuginfo`, `none`, `symbols`",
);
}

0 comments on commit 65fc4ce

Please sign in to comment.