Fix enum deserialization from env values

Fixes #31
This commit is contained in:
Lukas Kalbertodt
2023-07-02 10:13:04 +02:00
parent 29a5c05770
commit 4fdfb821dc
2 changed files with 31 additions and 1 deletions

13
src/env/mod.rs vendored
View File

@@ -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
@@ -127,7 +139,6 @@ impl<'de> serde::Deserializer<'de> for Deserializer {
ignored_any
// TODO: think about manually implementing these
enum
seq
tuple tuple_struct
}

19
tests/env.rs Normal file
View File

@@ -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 })));
}