use serde::Deserialize; use confique::{Config, Partial}; use pretty_assertions::assert_eq; #[derive(Debug, Deserialize)] enum Foo { A, B, C } #[test] fn enum_env() { #[derive(Config)] struct Conf { #[config(env = "FOO")] foo: Foo, } std::env::set_var("FOO", "B"); let conf = Conf::builder().env().load(); assert!(matches!(conf, Ok(Conf { foo: Foo::B }))); } fn my_parser(s: &str) -> Result { s.trim().parse() } fn my_parser2(s: &str) -> Result { if s.is_empty() { Ok(0) } else { s.trim().parse() } } #[test] fn empty_error_is_unset() { #[derive(Config)] #[config(partial_attr(derive(PartialEq, Debug)))] #[allow(dead_code)] struct Conf { #[config(env = "EMPTY_ERROR_IS_UNSET_FOO")] foo: u32, #[config(env = "EMPTY_ERROR_IS_UNSET_BAR", parse_env = my_parser)] bar: u32, #[config(env = "EMPTY_ERROR_IS_UNSET_BAZ")] baz: String, #[config(env = "EMPTY_ERROR_IS_UNSET_VALIDATE", validate(!validate.is_empty(), "bad"))] validate: String, #[config( env = "EMPTY_ERROR_IS_UNSET_VALIDATE_PARSE", parse_env = my_parser2, validate(*validate_parse != 0, "bad"), )] validate_parse: u32, } type Partial = ::Partial; std::env::set_var("EMPTY_ERROR_IS_UNSET_FOO", ""); assert_eq!(Partial::from_env().unwrap(), Partial { foo: None, bar: None, baz: None, validate: None, validate_parse: None, }); std::env::set_var("EMPTY_ERROR_IS_UNSET_BAR", ""); assert_eq!(Partial::from_env().unwrap(), Partial { foo: None, bar: None, baz: None, validate: None, validate_parse: None, }); std::env::set_var("EMPTY_ERROR_IS_UNSET_BAZ", ""); assert_eq!(Partial::from_env().unwrap(), Partial { foo: None, bar: None, baz: Some("".into()), validate: None, validate_parse: None, }); std::env::set_var("EMPTY_ERROR_IS_UNSET_VALIDATE", ""); assert_eq!(Partial::from_env().unwrap(), Partial { foo: None, bar: None, baz: Some("".into()), validate: None, validate_parse: None, }); std::env::set_var("EMPTY_ERROR_IS_UNSET_VALIDATE_PARSE", ""); assert_eq!(Partial::from_env().unwrap(), Partial { foo: None, bar: None, baz: Some("".into()), validate: None, validate_parse: None, }); }