Skip to content

Commit

Permalink
Auto merge of #8454 - GabrielMajeri:config-option-enum, r=ehuss
Browse files Browse the repository at this point in the history
Add support for deserializing enums in config files

Implements `deserialize_enum` functionality to allow config options which are Rust enums.

@ehuss The code currently has some `todo!`s because I'm not sure how the custom `Deserializer` is supposed to do error handling.

Fixes #8450
  • Loading branch information
bors committed Jul 7, 2020
2 parents 548eea7 + 65fc4ce commit 729e567
Show file tree
Hide file tree
Showing 2 changed files with 61 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
40 changes: 40 additions & 0 deletions tests/testsuite/config.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -1259,3 +1260,42 @@ 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);
}

#[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 729e567

Please sign in to comment.