chore: improve error message
This commit is contained in:
parent
4f6ca9d3eb
commit
b5fe39a3c3
2 changed files with 3 additions and 3 deletions
|
@ -176,9 +176,9 @@ fn derive_impl(input: syn::DeriveInput) -> syn::Result<TokenStream> {
|
|||
.any(|attr| attr.path().is_ident("relax"));
|
||||
|
||||
Ok(match (nested, get_generic_ty("Option", ty).is_some()) {
|
||||
(false, false) => quote! { #name: value.#name.ok_or(::relax::RequiredFieldNotSet(stringify!(#name)))? },
|
||||
(false, false) => quote! { #name: value.#name.ok_or(::relax::RequiredFieldNotSet(concat!("`", stringify!(#name), "` of ", stringify!(#base))))? },
|
||||
(false, true) => quote! { #name: value.#name },
|
||||
(true, false) => quote! { #name: value.#name.ok_or(::relax::RequiredFieldNotSet(stringify!(#name)))?.try_into()? },
|
||||
(true, false) => quote! { #name: value.#name.ok_or(::relax::RequiredFieldNotSet(concat!("`", stringify!(#name), "` of ", stringify!(#base))))?.try_into()? },
|
||||
(true, true) => quote! { #name: value.#name.map(|val| val.try_into()).transpose().ok().flatten() },
|
||||
})
|
||||
}).collect::<syn::Result<Vec<_>>>()?;
|
||||
|
|
|
@ -17,7 +17,7 @@ pub struct RequiredFieldNotSet(pub &'static str);
|
|||
|
||||
impl std::fmt::Display for RequiredFieldNotSet {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||
write!(f, "required field `{}` is not set", self.0)
|
||||
write!(f, "required field is not set ({})", self.0)
|
||||
}
|
||||
}
|
||||
impl std::error::Error for RequiredFieldNotSet {}
|
||||
|
|
Loading…
Reference in a new issue