1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-23 04:52:28 +00:00

improve the From impl for AuthErrorOrOther #20

This commit is contained in:
Dominic 2020-05-20 19:50:17 +02:00
parent 0b06528742
commit 912f030bfd
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
3 changed files with 26 additions and 9 deletions

View file

@ -41,6 +41,7 @@ uuid = { version = "0.8.1", optional = true }
diesel = { version = "1.4.4", features = ["postgres"] } diesel = { version = "1.4.4", features = ["postgres"] }
futures-executor = "0.3.4" futures-executor = "0.3.4"
paste = "0.1.12" paste = "0.1.12"
thiserror = "1.0.18"
trybuild = "1.0.26" trybuild = "1.0.26"
[features] [features]

View file

@ -167,7 +167,7 @@ impl ErrorVariant
}) })
} }
fn into_match_arm(self, krate : &TokenStream, enum_ident : &Ident) -> TokenStream fn into_match_arm(self, krate : &TokenStream, enum_ident : &Ident) -> Result<TokenStream>
{ {
let ident = &self.ident; let ident = &self.ident;
let fields_pat = self.fields_pat(); let fields_pat = self.fields_pat();
@ -185,21 +185,23 @@ impl ErrorVariant
}); });
// the response will come directly from the from_ty if present // the response will come directly from the from_ty if present
let res = match self.from_ty { let res = match (self.from_ty, status) {
Some((from_index, _)) => { (Some((from_index, _)), None) => {
let from_field = &self.fields[from_index].ident; let from_field = &self.fields[from_index].ident;
quote!(#from_field.into_response_error()) quote!(#from_field.into_response_error())
}, },
None => quote!(Ok(#krate::Response { (Some(_), Some(_)) => return Err(Error::new(ident.span(), "When #[from] is used, #[status] must not be used!")),
(None, Some(status)) => quote!(Ok(#krate::Response {
status: { #status }.into(), status: { #status }.into(),
body: #krate::gotham::hyper::Body::empty(), body: #krate::gotham::hyper::Body::empty(),
mime: None mime: None
})) })),
(None, None) => return Err(Error::new(ident.span(), "Missing #[status(code)] for this variant"))
}; };
quote! { Ok(quote! {
#enum_ident::#ident #fields_pat => #res #enum_ident::#ident #fields_pat => #res
} })
} }
fn were(&self) -> Option<TokenStream> fn were(&self) -> Option<TokenStream>
@ -293,7 +295,9 @@ pub fn expand_resource_error(input : DeriveInput) -> Result<TokenStream>
} }
let were = variants.iter().filter_map(|variant| variant.were()).collect::<Vec<_>>(); let were = variants.iter().filter_map(|variant| variant.were()).collect::<Vec<_>>();
let variants = variants.into_iter().map(|variant| variant.into_match_arm(&krate, &ident)); let variants = variants.into_iter()
.map(|variant| variant.into_match_arm(&krate, &ident))
.collect_to_result()?;
Ok(quote! { Ok(quote! {
#display_impl #display_impl

View file

@ -62,8 +62,9 @@ pub enum AuthErrorOrOther<E>
#[status(FORBIDDEN)] #[status(FORBIDDEN)]
#[display("Forbidden")] #[display("Forbidden")]
Forbidden, Forbidden,
#[status(INTERNAL_SERVER_ERROR)]
#[display("{0}")] #[display("{0}")]
Other(#[from] E) Other(E)
} }
impl<E> From<AuthError> for AuthErrorOrOther<E> impl<E> From<AuthError> for AuthErrorOrOther<E>
@ -76,6 +77,17 @@ impl<E> From<AuthError> for AuthErrorOrOther<E>
} }
} }
impl<E, F> From<F> for AuthErrorOrOther<E>
where
// TODO https://gitlab.com/msrd0/gotham-restful/-/issues/20
F : std::error::Error + Into<E>
{
fn from(err : F) -> Self
{
Self::Other(err.into())
}
}
/** /**
This return type can be used to map another `ResourceResult` that can only be returned if the This return type can be used to map another `ResourceResult` that can only be returned if the
client is authenticated. Otherwise, an empty _403 Forbidden_ response will be issued. Use can client is authenticated. Otherwise, an empty _403 Forbidden_ response will be issued. Use can