Skip to content

Commit

Permalink
test: add untagged enum test
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Nov 30, 2023
1 parent 84e38c9 commit 62be623
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"dataless",
"deserializations",
"docsrs",
"formatdoc",
"impls",
"indoc",
"ipnetwork",
Expand Down
2 changes: 2 additions & 0 deletions confik-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ syn = { version = "2", features = ["extra-traits"] }
[dev-dependencies]
assert_matches = "1.5"
confik = "0.11"
indoc = "2"
rustversion = "1"
serde = { version = "1", features = ["derive"] }
serde-bool = "0.1"
toml = "0.8"
trybuild = { version = "1", features = ["diff"] }
1 change: 1 addition & 0 deletions confik-macros/tests/trybuild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn compile_macros() {
t.pass("tests/trybuild/22-dataless-types.rs");
t.pass("tests/trybuild/23-where-clause.rs");
t.pass("tests/trybuild/24-field-try-from.rs");
t.pass("tests/trybuild/pass-enum-untagged.rs");

t.compile_fail("tests/trybuild/fail-default-parse.rs");
t.compile_fail("tests/trybuild/fail-default-invalid-expr.rs");
Expand Down
1 change: 1 addition & 0 deletions confik-macros/tests/trybuild/21-field-from.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//! Check that the `from` attribute works

use confik::{Configuration, TomlSource};

#[derive(Debug, Configuration, PartialEq, Eq)]
Expand Down
66 changes: 66 additions & 0 deletions confik-macros/tests/trybuild/pass-enum-untagged.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//! Checks "untagged enum" behavior / fall-through when deserializing.

use confik::{Configuration, TomlSource};

#[derive(Debug, serde::Deserialize)]
#[serde(untagged)]
enum S3Auth {
K8sServiceAccount {
use_k8s_service_account: serde_bool::True,
},

ApiKey {
access_key: String,
access_secret: String,
},
}

impl Configuration for S3Auth {
type Builder = Option<Self>;
}

#[derive(Debug, Configuration)]
struct Config {
s3_auth: S3Auth,
}

fn main() {
let config = indoc::formatdoc! {"
[s3_auth]
use_k8s_service_account = false
"};
Config::builder()
.override_with(TomlSource::new(config))
.try_build()
.expect_err("Successfully built with incorrect boolean inner value");

let config = indoc::formatdoc! {"
[s3_auth]
use_k8s_service_account = true
"};
let config = Config::builder()
.override_with(TomlSource::new(config))
.try_build()
.expect("Failed to build");
assert_matches::assert_matches!(config.s3_auth, S3Auth::K8sServiceAccount { .. });

let config = indoc::formatdoc! {"
[s3_auth]
access_key = \"foo\"
access_secret = \"bar\"
"};
let config = Config::builder()
.override_with(TomlSource::new(config))
.try_build()
.expect("Failed to build");
assert_matches::assert_matches!(config.s3_auth, S3Auth::ApiKey { .. });
}

// /// Only successfully deserializes from a `true` value boolean.
// struct True;

// impl serde::Deserialize for True {}

// impl config::Configuration for True {
// type Builder = Option<Self>;
// }

0 comments on commit 62be623

Please sign in to comment.