From 4c7be8d4fb0a1fa22eb2bf1bbef8dc3be4363c8d Mon Sep 17 00:00:00 2001 From: Gabriel Majeri Date: Sun, 5 Jul 2020 09:59:56 +0300 Subject: [PATCH 1/2] Add a test reproducing the issue --- tests/testsuite/config.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs index 5771440e9da..82a3f982fa0 100644 --- a/tests/testsuite/config.rs +++ b/tests/testsuite/config.rs @@ -1,5 +1,6 @@ //! Tests for config settings. +use cargo::core::profiles::Strip; use cargo::core::{enable_nightly_features, Shell}; use cargo::util::config::{self, Config, SslVersionConfig, StringList}; use cargo::util::interning::InternedString; @@ -1259,3 +1260,19 @@ fn string_list_advanced_env() { "error in environment variable `CARGO_KEY3`: expected string, found integer", ); } + +#[cargo_test] +fn parse_enum() { + write_config( + "\ +[profile.release] +strip = 'debuginfo' +", + ); + + let config = new_config(); + + let p: toml::TomlProfile = config.get("profile.release").unwrap(); + let strip = p.strip.unwrap(); + assert_eq!(strip, Strip::DebugInfo); +} From 65fc4cec8d6d5952f8433c0d16e02a5855a9aaaf Mon Sep 17 00:00:00 2001 From: Gabriel Majeri Date: Sun, 5 Jul 2020 10:00:08 +0300 Subject: [PATCH 2/2] Implement enum config options deserialization --- src/cargo/util/config/de.rs | 22 +++++++++++++++++++++- tests/testsuite/config.rs | 23 +++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/cargo/util/config/de.rs b/src/cargo/util/config/de.rs index ab8f96fbadf..1564a60f07d 100644 --- a/src/cargo/util/config/de.rs +++ b/src/cargo/util/config/de.rs @@ -195,11 +195,31 @@ impl<'de, 'config> de::Deserializer<'de> for Deserializer<'config> { } } + fn deserialize_enum( + self, + _name: &'static str, + _variants: &'static [&'static str], + visitor: V, + ) -> Result + 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 } } diff --git a/tests/testsuite/config.rs b/tests/testsuite/config.rs index 82a3f982fa0..29e79dba37a 100644 --- a/tests/testsuite/config.rs +++ b/tests/testsuite/config.rs @@ -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::("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`", + ); +}