mirror of
https://github.com/OMGeeky/confique.git
synced 2025-12-31 00:30:18 +01:00
Slightly simplify generated code
This commit is contained in:
@@ -30,9 +30,10 @@ fn gen_config_impl(input: &ir::Input) -> TokenStream {
|
||||
match f.kind {
|
||||
FieldKind::Nested { .. } => {
|
||||
quote! {
|
||||
confique::Config::from_partial(partial.#field_name).map_err(|e| {
|
||||
confique::internal::prepend_missing_value_error(e, #path)
|
||||
})?
|
||||
confique::internal::map_err_prefix_path(
|
||||
confique::Config::from_partial(partial.#field_name),
|
||||
#path,
|
||||
)?
|
||||
}
|
||||
}
|
||||
FieldKind::Leaf { kind: LeafKind::Optional { .. }, .. } => {
|
||||
@@ -40,8 +41,7 @@ fn gen_config_impl(input: &ir::Input) -> TokenStream {
|
||||
}
|
||||
FieldKind::Leaf { kind: LeafKind::Required { .. }, .. } => {
|
||||
quote! {
|
||||
partial.#field_name
|
||||
.ok_or(confique::internal::missing_value_error(#path.into()))?
|
||||
confique::internal::unwrap_or_missing_value_err(partial.#field_name, #path)?
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,17 +19,21 @@ where
|
||||
src.into_deserializer()
|
||||
}
|
||||
|
||||
pub fn missing_value_error(path: String) -> Error {
|
||||
ErrorInner::MissingValue(path).into()
|
||||
pub fn unwrap_or_missing_value_err<T>(value: Option<T>, path: &str) -> Result<T, Error> {
|
||||
match value {
|
||||
Some(v) => Ok(v),
|
||||
None => Err(ErrorInner::MissingValue(path.into()).into())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prepend_missing_value_error(e: Error, prefix: &str) -> Error {
|
||||
match *e.inner {
|
||||
ErrorInner::MissingValue(path) => {
|
||||
pub fn map_err_prefix_path<T>(res: Result<T, Error>, prefix: &str) -> Result<T, Error> {
|
||||
res.map_err(|e| {
|
||||
if let ErrorInner::MissingValue(path) = &*e.inner {
|
||||
ErrorInner::MissingValue(format!("{prefix}.{path}")).into()
|
||||
} else {
|
||||
e
|
||||
}
|
||||
e => e.into(),
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn from_env<'de, T: serde::Deserialize<'de>>(
|
||||
|
||||
Reference in New Issue
Block a user