chore: improve error message

This commit is contained in:
naskya 2024-07-16 11:04:30 +09:00
parent 4f6ca9d3eb
commit b5fe39a3c3
Signed by: naskya
GPG key ID: 712D413B3A9FED5C
2 changed files with 3 additions and 3 deletions

View file

@ -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<_>>>()?;

View file

@ -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 {}