Improve formatting slightly

This commit is contained in:
Lukas Kalbertodt
2022-11-06 10:37:58 +01:00
parent cb8f879b92
commit 1e74330f00
3 changed files with 32 additions and 31 deletions

View File

@@ -147,28 +147,25 @@ fn gen_partial_mod(input: &ir::Input) -> TokenStream {
}
});
let from_env_fields = input.fields.iter().map(|f| match &f.kind {
FieldKind::Leaf {
env: Some(key),
deserialize_with,
parse_env,
..
} => {
let field = format!("{}::{}", input.name, f.name);
match (parse_env, deserialize_with) {
(None, None) => quote! {
confique::internal::from_env(#key, #field)?
},
(None, Some(deserialize_with)) => quote! {
confique::internal::deserialize_from_env_with(#key, #field, #deserialize_with)?
},
(Some(parse_env), None) | (Some(parse_env), Some(_)) => quote! {
confique::internal::parse_from_env_with(#key, #field, #parse_env)?
},
let from_env_fields = input.fields.iter().map(|f| {
match &f.kind {
FieldKind::Leaf { env: Some(key), deserialize_with, parse_env, .. } => {
let field = format!("{}::{}", input.name, f.name);
match (parse_env, deserialize_with) {
(None, None) => quote! {
confique::internal::from_env(#key, #field)?
},
(None, Some(deserialize_with)) => quote! {
confique::internal::deserialize_from_env_with(#key, #field, #deserialize_with)?
},
(Some(parse_env), None) | (Some(parse_env), Some(_)) => quote! {
confique::internal::parse_from_env_with(#key, #field, #parse_env)?
},
}
}
FieldKind::Leaf { .. } => quote! { None },
FieldKind::Nested { .. } => quote! { confique::Partial::from_env()? },
}
FieldKind::Leaf { .. } => quote! { None },
FieldKind::Nested { .. } => quote! { confique::Partial::from_env()? },
});
let fallbacks = input.fields.iter().map(|f| {

View File

@@ -71,21 +71,24 @@ impl Field {
"A `parse_env` attribute, cannot be provided without the `env` attribute",
));
}
let kind = match unwrap_option(&field.ty) {
Some(_) if attrs.default.is_some() => {
return Err(Error::new(
field.ident.span(),
"optional fields (type `Option<_>`) cannot have default \
values (`#[config(default = ...)]`)",
));
},
Some(inner) => LeafKind::Optional { inner_ty: inner.clone() },
None => LeafKind::Required { default: attrs.default, ty: field.ty },
};
FieldKind::Leaf {
env: attrs.env,
deserialize_with: attrs.deserialize_with,
parse_env: attrs.parse_env,
kind: match unwrap_option(&field.ty) {
Some(_inner) if attrs.default.is_some() => {
return Err(Error::new(
field.ident.span(),
"optional fields (type `Option<_>`) cannot have default \
values (`#[config(default = ...)]`)",
));
},
Some(inner) => LeafKind::Optional { inner_ty: inner.clone() },
None => LeafKind::Required { default: attrs.default, ty: field.ty },
},
kind,
}
};

View File

@@ -128,6 +128,7 @@ impl<'de> serde::Deserializer<'de> for Deserializer {
}
}
#[cfg(test)]
mod tests {
use super::*;