Skip to content

Commit

Permalink
Fix enum deserialization from env values
Browse files Browse the repository at this point in the history
Fixes #31
  • Loading branch information
LukasKalbertodt committed Jul 2, 2023
1 parent 29a5c05 commit 4fdfb82
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,18 @@ impl<'de> serde::Deserializer<'de> for Deserializer {
visitor.visit_newtype_struct(self)
}

fn deserialize_enum<V>(
self,
_name: &str,
_variants: &'static [&'static str],
visitor: V,
) -> Result<V::Value, Self::Error>
where
V: serde::de::Visitor<'de>,
{
visitor.visit_enum(self.value.into_deserializer())
}

serde::forward_to_deserialize_any! {
char str string
bytes byte_buf
Expand All @@ -127,7 +139,6 @@ impl<'de> serde::Deserializer<'de> for Deserializer {
ignored_any

// TODO: think about manually implementing these
enum
seq
tuple tuple_struct
}
Expand Down
19 changes: 19 additions & 0 deletions tests/env.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use serde::Deserialize;
use confique::{Config};

#[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 })));
}

0 comments on commit 4fdfb82

Please sign in to comment.