Fix using env with deserialize_with

Fixes #2

I simply forgot to use the `deserialize_with` attribute for the env
deserialization. The previous code was somewhat weirdly coded in that
we would always deserialize `Option<T>` (as it's a partial type) and
the "env variable not present" info would travel through the
deserializer to the `Option<T> as Deserialize` impl. Now it's more
straight forward.
This commit is contained in:
Lukas Kalbertodt
2022-10-14 16:21:34 +02:00
parent 04f7f6b3be
commit eb03488973
4 changed files with 85 additions and 75 deletions

View File

@@ -34,6 +34,12 @@ pub(crate) enum ErrorInner {
err: Box<dyn std::error::Error + Send + Sync>,
},
/// When the env variable `key` is not Unicode.
EnvNotUnicode {
field: String,
key: String,
},
/// When deserialization via `env` fails. The string is what is passed to
/// `serde::de::Error::custom`.
EnvDeserialization {
@@ -72,6 +78,7 @@ impl std::error::Error for Error {
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::Deserialization { err, .. } => Some(&**err),
ErrorInner::MissingValue(_) => None,
ErrorInner::EnvNotUnicode { .. } => None,
ErrorInner::EnvDeserialization { .. } => None,
#[cfg(any(feature = "toml", feature = "yaml"))]
ErrorInner::UnsupportedFileFormat { .. } => None,
@@ -108,6 +115,14 @@ impl fmt::Display for Error {
ErrorInner::Deserialization { source: None, .. } => {
std::write!(f, "failed to deserialize configuration")
}
ErrorInner::EnvNotUnicode { field, key } => {
std::write!(f,
"failed to load value `{}` from environment variable `{}`: \
value is not valid unicode",
field,
key,
)
}
ErrorInner::EnvDeserialization { field, key, msg } => {
std::write!(f,
"failed to deserialize value `{}` from environment variable `{}`: {}",