Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion confik-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,8 +267,13 @@ impl VariantImplementer {

let string = ident.to_string();

// A selected variant counts as data, even if it has no fields.
quote_spanned! {var_impl.span() =>
Self::#ident #bracketed_extract_us_fields => false #( | #contains_non_secret_data.map_err(|err| err.prepend(#string))? )*
Self::#ident #bracketed_extract_us_fields => {
#( #contains_non_secret_data.map_err(|err| err.prepend(#string))?; )*

true
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions confik/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Preserve explicitly selected enum unit variants.
- Reject explicitly selected enum variants in `#[confik(secret)]` fields when the source does not allow secrets.
- Upgrade `syn` dependency to `3`.
- Minimum supported Rust version (MSRV) is now 1.88.

Expand Down
23 changes: 23 additions & 0 deletions confik/tests/defaulting_containers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,29 @@ mod struct_default_merge {
}
}

#[cfg(feature = "toml")]
mod enum_defaults {
use confik::{Configuration, TomlSource};

use crate::TargetEnum;

#[test]
fn unit_enum_variant_overrides_field_default() {
#[derive(Configuration)]
struct Config {
#[confik(default = TargetEnum::First)]
value: TargetEnum,
}

let config = Config::builder()
.override_with(TomlSource::new(r#"value = "Second""#))
.try_build()
.expect("TOML deserialization should succeed");

assert_eq!(config.value, TargetEnum::Second);
}
}

#[derive(Debug, Configuration, PartialEq, Eq, Default)]
struct Config {
#[confik(default = 0)]
Expand Down
24 changes: 24 additions & 0 deletions confik/tests/secret/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,30 @@ mod toml {
);
}

#[test]
fn unit_enum_variant_rejected_from_non_secret_source() {
use confik::Error;

use crate::TargetEnum;

#[derive(Configuration)]
struct Config {
#[confik(secret)]
value: TargetEnum,
}

let err = Config::builder()
.override_with(TomlSource::new(r#"value = "Second""#))
.try_build()
.map(|config| config.value)
.expect_err("TOML must reject a secret unit enum variant");

assert_matches!(
err,
Error::UnexpectedSecret(path, _) if path.to_string().contains("`value`")
);
}

/// This functions and all tests using are to catch the issue in FUT-5298
/// in which depending on ordering a non-secret `Source` may not be caught
fn check_secret_error_seq_propagation<T>(expected_path: &str)
Expand Down
Loading