Skip to content

Commit

Permalink
Fixed tests, and simplify error messages.
Browse files Browse the repository at this point in the history
I did an early return when the tuple check fails, cause the `check_output_impls_into_response` would clutter the error message in this case.
  • Loading branch information
brian030128 committed Nov 9, 2023
1 parent 136b423 commit e6a639d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 59 deletions.
18 changes: 10 additions & 8 deletions axum-macros/src/debug_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ pub(crate) fn expand(attr: Attrs, item_fn: ItemFn) -> TokenStream {
let check_extractor_count = check_extractor_count(&item_fn);
let check_path_extractor = check_path_extractor(&item_fn);
let check_output_tuples = check_output_tuples(&item_fn);
if !check_output_tuples.is_empty() {
return check_output_tuples;
}
let check_output_impls_into_response = check_output_impls_into_response(&item_fn);

// If the function is generic, we can't reliably check its inputs or whether the future it
Expand Down Expand Up @@ -292,28 +295,28 @@ fn check_inputs_impls_from_request(item_fn: &ItemFn, state_ty: Type) -> TokenStr
///last element => IntoResponse
///other elements => IntoResponseParts
///the max numbers of IntoResponseParts(16)
fn check_output_tuples(item_fn: &ItemFn) -> Option<TokenStream> {
fn check_output_tuples(item_fn: &ItemFn) -> TokenStream {
//Extract tuple types
let elements = match &item_fn.sig.output {
ReturnType::Type(_, ty) => match &**ty {
Type::Tuple(tuple) => &tuple.elems,
_ => return None,
_ => return quote! {},
},
ReturnType::Default => return None,
ReturnType::Default => return quote! {},
};
let handler_ident = &item_fn.sig.ident;

//Amount of IntoRequestParts
let mut parts_amount: i8 = 0;

let token_stream = WithPosition::new(elements.iter())
WithPosition::new(elements.iter())
.enumerate()
.map(|(_idx, arg)| match &arg {
//First element type in the tuple
Position::First(ty) => {

extract_clean_typename(ty)
.filter(|typename| {
matches!(&**typename, "Parts" | "Response" | "StatusCode")
!matches!(&**typename, "Parts" | "Response" | "StatusCode")
})
.map(|_| {
parts_amount += 1;
Expand Down Expand Up @@ -344,8 +347,7 @@ fn check_output_tuples(item_fn: &ItemFn) -> Option<TokenStream> {
ty,
),
})
.collect::<TokenStream>();
Some(token_stream)
.collect::<TokenStream>()
}

fn _check_into_response_not_parts(ty: &Type) -> Option<&'static str> {
Expand Down
36 changes: 0 additions & 36 deletions axum-macros/tests/debug_handler/fail/output_tuple_too_many.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,3 @@ error: Output Tuple cannot have more than 16 arguments.
24 | | axum::http::StatusCode,
25 | | ) {
| |_^

error[E0277]: the trait bound `(StatusCode, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, StatusCode): IntoResponse` is not satisfied
--> tests/debug_handler/fail/output_tuple_too_many.rs:5:6
|
5 | ) -> (
| ______^
6 | | axum::http::StatusCode,
7 | | AppendHeaders<[(axum::http::HeaderName, &'static str); 1]>,
8 | | AppendHeaders<[(axum::http::HeaderName, &'static str); 1]>,
... |
24 | | axum::http::StatusCode,
25 | | ) {
| |_^ the trait `IntoResponse` is not implemented for `(StatusCode, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, AppendHeaders<[(HeaderName, &str); 1]>, StatusCode)`
|
= help: the following other types implement trait `IntoResponse`:
()
(R,)
(Response<()>, R)
(Response<()>, T1, R)
(Response<()>, T1, T2, R)
(Response<()>, T1, T2, T3, R)
(Response<()>, T1, T2, T3, T4, R)
(Response<()>, T1, T2, T3, T4, T5, R)
and $N others
note: required by a bound in `__axum_macros_check_handler_into_response::{closure#0}::check`
--> tests/debug_handler/fail/output_tuple_too_many.rs:5:6
|
5 | ) -> (
| ______^
6 | | axum::http::StatusCode,
7 | | AppendHeaders<[(axum::http::HeaderName, &'static str); 1]>,
8 | | AppendHeaders<[(axum::http::HeaderName, &'static str); 1]>,
... |
24 | | axum::http::StatusCode,
25 | | ) {
| |_^ required by this bound in `check`
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ error[E0277]: the trait bound `NotIntoResponse: IntoResponse` is not satisfied
| ^^^^^^^^^^^^^^^^^ the trait `IntoResponse` is not implemented for `NotIntoResponse`
|
= help: the following other types implement trait `IntoResponse`:
&'static [u8; N]
&'static [u8]
&'static str
()
(R,)
(Response<()>, R)
(Response<()>, T1, R)
(Response<()>, T1, T2, R)
Box<str>
Box<[u8]>
axum::body::Bytes
Body
axum::extract::rejection::FailedToBufferBody
axum::extract::rejection::LengthLimitError
axum::extract::rejection::UnknownBodyError
axum::extract::rejection::InvalidUtf8
and $N others
note: required by a bound in `__axum_macros_check_handler_into_response::{closure#0}::check`
--> tests/debug_handler/fail/single_wrong_return_tuple.rs:6:23
Expand Down
14 changes: 7 additions & 7 deletions axum-macros/tests/debug_handler/fail/wrong_return_tuple.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ error[E0277]: the trait bound `CustomIntoResponse: IntoResponseParts` is not sat
| ^^^^^^^^^^^^^^^^^^ the trait `IntoResponseParts` is not implemented for `CustomIntoResponse`
|
= help: the following other types implement trait `IntoResponseParts`:
AppendHeaders<I>
HeaderMap
Extension<T>
Extensions
Option<T>
[(K, V); N]
(T1,)
(T1, T2)
(T1, T2, T3)
(T1, T2, T3, T4)
(T1, T2, T3, T4, T5)
(T1, T2, T3, T4, T5, T6)
(T1, T2, T3, T4, T5, T6, T7)
(T1, T2, T3, T4, T5, T6, T7, T8)
(T1, T2, T3, T4, T5, T6, T7, T8, T9)
and $N others
= help: see issue #48214
= help: add `#![feature(trivial_bounds)]` to the crate attributes to enable

0 comments on commit e6a639d

Please sign in to comment.