Skip to content

Commit

Permalink
improve error message when auth data is missing clone impl
Browse files Browse the repository at this point in the history
  • Loading branch information
msrd0 committed May 22, 2021
1 parent 47e3313 commit 45ccf8c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
9 changes: 7 additions & 2 deletions derive/src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ fn interpret_arg(_index: usize, arg: &PatType) -> Result<HandlerArg> {
#[cfg(feature = "openapi")]
fn expand_operation_verb(operation_verb: TokenStream) -> Option<TokenStream> {
Some(quote! {
fn operation_verb() -> Option<&'static str> {
fn operation_verb() -> ::core::option::Option<&'static ::core::primitive::str> {
#operation_verb
}
})
Expand Down Expand Up @@ -531,9 +531,14 @@ fn expand_endpoint_type(mut ty: EndpointType, attrs: AttributeArgs, fun: &ItemFn
let mut state_block = quote!();
if let Some(arg) = args.iter().find(|arg| arg.ty.is_auth_status()) {
let auth_ty = arg.ty.quote_ty();
let auth_borrow = quote_spanned! { auth_ty.span() =>
<#auth_ty as ::core::clone::Clone>::clone(
<#auth_ty as ::gotham_restful::gotham::state::FromState>::borrow_from(state)
)
};
state_block = quote! {
#state_block
let auth: #auth_ty = state.borrow::<#auth_ty>().clone();
let auth: #auth_ty = #auth_borrow;
}
}

Expand Down
20 changes: 20 additions & 0 deletions tests/ui/endpoint/auth_data_non_clone.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use gotham_restful::*;
use serde::Deserialize;

#[derive(Resource)]
#[resource(read_all)]
struct FooResource;

#[derive(Deserialize)]
struct AuthData {
iat: u64,
exp: u64
}

#[read_all]
async fn read_all(auth: AuthStatus<AuthData>) -> Result<NoContent, AuthError> {
auth.ok()?;
Ok(NoContent::default())
}

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/endpoint/auth_data_non_clone.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error[E0277]: the trait bound `AuthData: Clone` is not satisfied
--> $DIR/auth_data_non_clone.rs:15:25
|
15 | async fn read_all(auth: AuthStatus<AuthData>) -> Result<NoContent, AuthError> {
| ^^^^^^^^^^ the trait `Clone` is not implemented for `AuthData`
|
= note: required because of the requirements on the impl of `Clone` for `gotham_restful::AuthStatus<AuthData>`
= note: required by `clone`

0 comments on commit 45ccf8c

Please sign in to comment.