// `Other` is a single-variant enum since using a newtype struct results in the name being lost entirely, which I believe is a serde issue.
#[derive(Debug, Serialize, Deserialize)]
enum Other {
Env(String),
}
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum MaybeEnv {
Value(String),
Other(Other),
}
This is set up like this to allow using a raw String literal or a fallback which executes different behavior (in this case reading the string from the environment), but only if it's wrapped in Env.
However, when deserializing Env("bar") it fails with "data did not match any variant of untagged enum MaybeEnv" instead of MaybeEnv::Other(Other::Env("bar")). I'm also unable to find a string which properly deserializes to this value at all.
I'm unsure if this is a serde limitation, but it's relatively surprising behavior since it serializes properly but fails to deserialize.
Consider the following implementation
This is set up like this to allow using a raw String literal or a fallback which executes different behavior (in this case reading the string from the environment), but only if it's wrapped in
Env
.The following works properly:
MaybeEnv::Value("foo")
outputs"foo"
MaybeEnv::Other(Other::Env("bar"))
outputsEnv("bar")
"foo"
results inMaybeEnv::Value("foo")
However, when deserializing
Env("bar")
it fails with"data did not match any variant of untagged enum MaybeEnv"
instead ofMaybeEnv::Other(Other::Env("bar"))
. I'm also unable to find a string which properly deserializes to this value at all.I'm unsure if this is a serde limitation, but it's relatively surprising behavior since it serializes properly but fails to deserialize.