mirror of
https://github.com/tokio-rs/axum.git
synced 2025-03-13 19:27:53 +01:00
Clean up axum-debug crate (#498)
* Clean up axum-debug crate Mostly just brings the crate more in line with the rest of the crates in the workspace. I've also removed the axum-debug-macros crate since axum-debug only contained one re-export from axum-debug-macros. So we didn't need two crates. Can always bring the "backend" crate back if we need things in axum-debug that aren't proc-macros. * Just testing: This should make CI fail * Misc CI clean up * fix intentional breakage * fix changelog * Remove rustfmt config for now as it gives warnings on stable * Fix paths
This commit is contained in:
parent
f6b47478da
commit
5da3f34b3e
14 changed files with 354 additions and 438 deletions
15
.github/workflows/CI.yml
vendored
15
.github/workflows/CI.yml
vendored
|
@ -41,9 +41,8 @@ jobs:
|
|||
profile: minimal
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
- name: cargo doc
|
||||
working-directory: ${{ matrix.subcrate }}
|
||||
env:
|
||||
RUSTDOCFLAGS: "-D broken-intra-doc-links"
|
||||
RUSTDOCFLAGS: "-D broken-intra-doc-links"
|
||||
run: cargo doc --all-features --no-deps
|
||||
|
||||
cargo-hack:
|
||||
|
@ -60,7 +59,6 @@ jobs:
|
|||
run: |
|
||||
curl -LsSf https://github.com/taiki-e/cargo-hack/releases/latest/download/cargo-hack-x86_64-unknown-linux-gnu.tar.gz | tar xzf - -C ~/.cargo/bin
|
||||
- name: cargo hack check
|
||||
working-directory: ${{ matrix.subcrate }}
|
||||
run: cargo hack check --each-feature --no-dev-deps --all
|
||||
|
||||
test-versions:
|
||||
|
@ -88,14 +86,11 @@ jobs:
|
|||
test-msrv:
|
||||
needs: check
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
rust: [1.54]
|
||||
steps:
|
||||
- uses: actions/checkout@master
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
toolchain: ${{ matrix.rust }}
|
||||
toolchain: 1.54
|
||||
override: true
|
||||
profile: minimal
|
||||
- uses: Swatinem/rust-cache@v1
|
||||
|
@ -103,7 +98,11 @@ jobs:
|
|||
uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: -p axum --all-features --all-targets
|
||||
args: >
|
||||
-p axum
|
||||
-p axum-debug
|
||||
-p axum-handle-error-extract
|
||||
--all-features --all-targets
|
||||
|
||||
test-docs:
|
||||
needs: check
|
||||
|
|
|
@ -1,2 +0,0 @@
|
|||
# Merge multiple `use`s from the same crate
|
||||
imports_granularity = "Crate"
|
|
@ -2,7 +2,6 @@
|
|||
members = [
|
||||
"axum",
|
||||
"axum-debug",
|
||||
"axum-debug-macros",
|
||||
"axum-handle-error-extract",
|
||||
"examples/*",
|
||||
]
|
||||
|
|
|
@ -1,13 +0,0 @@
|
|||
# Changelog
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||
|
||||
# Unreleased
|
||||
|
||||
- **breaking:** Removed `debug_router` macro.
|
||||
|
||||
# 0.1.0 (6. October 2021)
|
||||
|
||||
- Initial release.
|
|
@ -1,20 +0,0 @@
|
|||
[package]
|
||||
authors = ["Programatik <programatik29@gmail.com>"]
|
||||
categories = ["development-tools::debugging"]
|
||||
description = "Macros for axum-debug crate."
|
||||
edition = "2018"
|
||||
homepage = "https://github.com/tokio-rs/axum"
|
||||
keywords = ["axum", "debugging", "debug"]
|
||||
license = "MIT"
|
||||
name = "axum-debug-macros"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/tokio-rs/axum"
|
||||
version = "0.1.0"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
proc-macro2 = "1"
|
||||
quote = "1"
|
||||
syn = { version = "1", features = ["full"] }
|
|
@ -1,7 +0,0 @@
|
|||
Copyright 2021 Axum Debug Contributors
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@ -1,23 +0,0 @@
|
|||
[](https://choosealicense.com/licenses/mit/)
|
||||
[](https://crates.io/crates/axum-debug-macros)
|
||||
[](https://docs.rs/axum-debug-macros/)
|
||||
|
||||
# axum-debug-macros
|
||||
|
||||
Procedural macros for [`axum-debug`] crate.
|
||||
|
||||
## Safety
|
||||
|
||||
This crate uses `#![forbid(unsafe_code)]` to ensure everything is implemented
|
||||
in 100% safe Rust.
|
||||
|
||||
## Performance
|
||||
|
||||
This crate have no effect when using release profile. (eg. `cargo build
|
||||
--release`)
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the [MIT license](LICENSE).
|
||||
|
||||
[`axum-debug`]: https://crates.io/crates/axum-debug
|
|
@ -1,305 +0,0 @@
|
|||
//! Procedural macros for [`axum-debug`] crate.
|
||||
//!
|
||||
//! [`axum-debug`]: https://crates.io/crates/axum-debug
|
||||
|
||||
#![warn(
|
||||
clippy::all,
|
||||
clippy::dbg_macro,
|
||||
clippy::todo,
|
||||
clippy::mem_forget,
|
||||
rust_2018_idioms,
|
||||
future_incompatible,
|
||||
nonstandard_style,
|
||||
missing_debug_implementations,
|
||||
missing_docs
|
||||
)]
|
||||
#![deny(unreachable_pub, private_in_public)]
|
||||
#![forbid(unsafe_code)]
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
|
||||
/// Generates better error messages when applied to a handler function.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Function is not async:
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// #[debug_handler]
|
||||
/// fn handler() -> &'static str {
|
||||
/// "Hello, world"
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ```text
|
||||
/// error: handlers must be async functions
|
||||
/// --> main.rs:xx:1
|
||||
/// |
|
||||
/// xx | fn handler() -> &'static str {
|
||||
/// | ^^
|
||||
/// ```
|
||||
///
|
||||
/// Wrong return type:
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// #[debug_handler]
|
||||
/// async fn handler() -> bool {
|
||||
/// false
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ```text
|
||||
/// error[E0277]: the trait bound `bool: IntoResponse` is not satisfied
|
||||
/// --> main.rs:xx:23
|
||||
/// |
|
||||
/// xx | async fn handler() -> bool {
|
||||
/// | ^^^^
|
||||
/// | |
|
||||
/// | the trait `IntoResponse` is not implemented for `bool`
|
||||
/// ```
|
||||
///
|
||||
/// Wrong extractor:
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// #[debug_handler]
|
||||
/// async fn handler(a: bool) -> String {
|
||||
/// format!("Can I extract a bool? {}", a)
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ```text
|
||||
/// error[E0277]: the trait bound `bool: FromRequest` is not satisfied
|
||||
/// --> main.rs:xx:21
|
||||
/// |
|
||||
/// xx | async fn handler(a: bool) -> String {
|
||||
/// | ^^^^
|
||||
/// | |
|
||||
/// | the trait `FromRequest` is not implemented for `bool`
|
||||
/// ```
|
||||
///
|
||||
/// Too many extractors:
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// #[debug_handler]
|
||||
/// async fn handler(
|
||||
/// a: String,
|
||||
/// b: String,
|
||||
/// c: String,
|
||||
/// d: String,
|
||||
/// e: String,
|
||||
/// f: String,
|
||||
/// g: String,
|
||||
/// h: String,
|
||||
/// i: String,
|
||||
/// j: String,
|
||||
/// k: String,
|
||||
/// l: String,
|
||||
/// m: String,
|
||||
/// n: String,
|
||||
/// o: String,
|
||||
/// p: String,
|
||||
/// q: String,
|
||||
/// ) {}
|
||||
/// ```
|
||||
///
|
||||
/// ```text
|
||||
/// error: too many extractors. 16 extractors are allowed
|
||||
/// note: you can nest extractors like "a: (Extractor, Extractor), b: (Extractor, Extractor)"
|
||||
/// --> main.rs:xx:5
|
||||
/// |
|
||||
/// xx | / a: String,
|
||||
/// xx | | b: String,
|
||||
/// xx | | c: String,
|
||||
/// xx | | d: String,
|
||||
/// ... |
|
||||
/// xx | | p: String,
|
||||
/// xx | | q: String,
|
||||
/// | |______________^
|
||||
/// ```
|
||||
///
|
||||
/// Future is not [`Send`]:
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// #[debug_handler]
|
||||
/// async fn handler() {
|
||||
/// let not_send = std::rc::Rc::new(());
|
||||
///
|
||||
/// async{}.await;
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ```text
|
||||
/// error: future cannot be sent between threads safely
|
||||
/// --> main.rs:xx:10
|
||||
/// |
|
||||
/// xx | async fn handler() {
|
||||
/// | ^^^^^^^
|
||||
/// | |
|
||||
/// | future returned by `handler` is not `Send`
|
||||
/// ```
|
||||
///
|
||||
/// [`Send`]: Send
|
||||
#[proc_macro_attribute]
|
||||
pub fn debug_handler(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
#[cfg(not(debug_assertions))]
|
||||
return input;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
return debug::apply_debug_handler(input);
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
mod debug {
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::Span;
|
||||
use quote::quote_spanned;
|
||||
use syn::{parse_macro_input, FnArg, Ident, ItemFn, ReturnType, Signature};
|
||||
|
||||
pub(crate) fn apply_debug_handler(input: TokenStream) -> TokenStream {
|
||||
let function = parse_macro_input!(input as ItemFn);
|
||||
|
||||
let vis = &function.vis;
|
||||
let sig = &function.sig;
|
||||
let ident = &sig.ident;
|
||||
let span = ident.span();
|
||||
let len = sig.inputs.len();
|
||||
let generics = create_generics(len);
|
||||
let params = sig.inputs.iter().map(|fn_arg| {
|
||||
if let FnArg::Typed(pat_type) = fn_arg {
|
||||
&pat_type.pat
|
||||
} else {
|
||||
panic!("not a handler function");
|
||||
}
|
||||
});
|
||||
let block = &function.block;
|
||||
|
||||
if let Err(error) = async_check(sig) {
|
||||
return error;
|
||||
}
|
||||
|
||||
if let Err(error) = param_limit_check(sig) {
|
||||
return error;
|
||||
}
|
||||
|
||||
let check_trait = check_trait_code(sig, &generics);
|
||||
let check_return = check_return_code(sig, &generics);
|
||||
let check_params = check_params_code(sig, &generics);
|
||||
|
||||
let expanded = quote_spanned! {span=>
|
||||
#vis #sig {
|
||||
#check_trait
|
||||
#check_return
|
||||
#(#check_params)*
|
||||
|
||||
#sig #block
|
||||
|
||||
#ident(#(#params),*).await
|
||||
}
|
||||
};
|
||||
|
||||
expanded.into()
|
||||
}
|
||||
|
||||
fn create_generics(len: usize) -> Vec<Ident> {
|
||||
let mut vec = Vec::new();
|
||||
for i in 1..=len {
|
||||
vec.push(Ident::new(&format!("T{}", i), Span::call_site()));
|
||||
}
|
||||
vec
|
||||
}
|
||||
|
||||
fn async_check(sig: &Signature) -> Result<(), TokenStream> {
|
||||
if sig.asyncness.is_none() {
|
||||
let error = syn::Error::new_spanned(sig.fn_token, "handlers must be async functions")
|
||||
.to_compile_error()
|
||||
.into();
|
||||
|
||||
return Err(error);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn param_limit_check(sig: &Signature) -> Result<(), TokenStream> {
|
||||
if sig.inputs.len() > 16 {
|
||||
let msg = "too many extractors. 16 extractors are allowed\n\
|
||||
note: you can nest extractors like \"a: (Extractor, Extractor), b: (Extractor, Extractor)\"";
|
||||
|
||||
let error = syn::Error::new_spanned(&sig.inputs, msg)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
|
||||
return Err(error);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_trait_code(sig: &Signature, generics: &[Ident]) -> proc_macro2::TokenStream {
|
||||
let ident = &sig.ident;
|
||||
let span = ident.span();
|
||||
|
||||
quote_spanned! {span=>
|
||||
{
|
||||
debug_handler(#ident);
|
||||
|
||||
fn debug_handler<F, Fut, #(#generics),*>(_f: F)
|
||||
where
|
||||
F: ::std::ops::FnOnce(#(#generics),*) -> Fut + Clone + Send + Sync + 'static,
|
||||
Fut: ::std::future::Future + Send,
|
||||
{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_return_code(sig: &Signature, generics: &[Ident]) -> proc_macro2::TokenStream {
|
||||
let span = match &sig.output {
|
||||
ReturnType::Default => syn::Error::new_spanned(&sig.output, "").span(),
|
||||
ReturnType::Type(_, t) => syn::Error::new_spanned(t, "").span(),
|
||||
};
|
||||
let ident = &sig.ident;
|
||||
|
||||
quote_spanned! {span=>
|
||||
{
|
||||
debug_handler(#ident);
|
||||
|
||||
fn debug_handler<F, Fut, Res, #(#generics),*>(_f: F)
|
||||
where
|
||||
F: ::std::ops::FnOnce(#(#generics),*) -> Fut,
|
||||
Fut: ::std::future::Future<Output = Res>,
|
||||
Res: ::axum_debug::axum::response::IntoResponse,
|
||||
{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_params_code(sig: &Signature, generics: &[Ident]) -> Vec<proc_macro2::TokenStream> {
|
||||
let mut vec = Vec::new();
|
||||
|
||||
let ident = &sig.ident;
|
||||
|
||||
for (i, generic) in generics.iter().enumerate() {
|
||||
let span = match &sig.inputs[i] {
|
||||
FnArg::Typed(pat_type) => syn::Error::new_spanned(&pat_type.ty, "").span(),
|
||||
_ => panic!("not a handler"),
|
||||
};
|
||||
|
||||
let token_stream = quote_spanned! {span=>
|
||||
{
|
||||
debug_handler(#ident);
|
||||
|
||||
fn debug_handler<F, Fut, #(#generics),*>(_f: F)
|
||||
where
|
||||
F: ::std::ops::FnOnce(#(#generics),*) -> Fut,
|
||||
Fut: ::std::future::Future,
|
||||
#generic: ::axum_debug::axum::extract::FromRequest + Send,
|
||||
{}
|
||||
}
|
||||
};
|
||||
|
||||
vec.push(token_stream);
|
||||
}
|
||||
|
||||
vec
|
||||
}
|
||||
}
|
|
@ -1,4 +1,5 @@
|
|||
# Changelog
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
||||
|
|
|
@ -11,6 +11,11 @@ readme = "README.md"
|
|||
repository = "https://github.com/tokio-rs/axum"
|
||||
version = "0.1.0"
|
||||
|
||||
[lib]
|
||||
proc-macro = true
|
||||
|
||||
[dependencies]
|
||||
axum = { path = "../axum" }
|
||||
axum-debug-macros = { path = "../axum-debug-macros" }
|
||||
proc-macro2 = "1.0"
|
||||
quote = "1.0"
|
||||
syn = { version = "1.0", features = ["full"] }
|
||||
axum = { path = "../axum", version = "0.3" }
|
||||
|
|
|
@ -1,63 +1,42 @@
|
|||
[](https://choosealicense.com/licenses/mit/)
|
||||
[](https://crates.io/crates/axum-debug)
|
||||
[](https://docs.rs/axum-debug/)
|
||||
|
||||
# axum-debug
|
||||
|
||||
[](https://github.com/tokio-rs/axum-debug/actions/workflows/CI.yml)
|
||||
[](https://crates.io/crates/axum-debug)
|
||||
[](https://docs.rs/axum-debug)
|
||||
|
||||
This is a debugging crate that provides better error messages for [`axum`]
|
||||
framework.
|
||||
|
||||
[`axum`] is a great framework for developing web applications. But when you
|
||||
make a mistake, error messages can be really complex and long. It can take a
|
||||
long time for you to figure out what is wrong in your code. This crate provides
|
||||
utilities to generate better error messages in case you make a mistake.
|
||||
|
||||
## Usage Example
|
||||
|
||||
Will fail with a better error message:
|
||||
|
||||
```rust
|
||||
use axum::{routing::get, Router};
|
||||
use axum_debug::debug_handler;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let app = Router::new().route("/", get(handler));
|
||||
|
||||
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
|
||||
.serve(app.into_make_service())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[debug_handler]
|
||||
async fn handler() -> bool {
|
||||
false
|
||||
}
|
||||
```
|
||||
|
||||
Error message:
|
||||
|
||||
```
|
||||
error[E0277]: the trait bound `bool: IntoResponse` is not satisfied
|
||||
--> main.rs:xx:23
|
||||
|
|
||||
xx | async fn handler() -> bool {
|
||||
| ^^^^
|
||||
| |
|
||||
| the trait `IntoResponse` is not implemented for `bool`
|
||||
```
|
||||
More information about this crate can be found in the [crate documentation][docs].
|
||||
|
||||
## Safety
|
||||
|
||||
This crate uses `#![forbid(unsafe_code)]` to ensure everything is implemented in 100% safe Rust.
|
||||
|
||||
## Performance
|
||||
## Minimum supported Rust version
|
||||
|
||||
Macros in this crate have no effect when using release profile. (eg. `cargo build --release`)
|
||||
axum-handle-error-extract's MSRV is 1.54.
|
||||
|
||||
## Getting Help
|
||||
|
||||
You're also welcome to ask in the [Discord channel][chat] or open an [issue]
|
||||
with your question.
|
||||
|
||||
## Contributing
|
||||
|
||||
:balloon: Thanks for your help improving the project! We are so happy to have
|
||||
you! We have a [contributing guide][contributing] to help you get involved in the
|
||||
`axum` project.
|
||||
|
||||
## License
|
||||
|
||||
This project is licensed under the [MIT license](LICENSE).
|
||||
This project is licensed under the [MIT license][license].
|
||||
|
||||
### Contribution
|
||||
|
||||
Unless you explicitly state otherwise, any contribution intentionally submitted
|
||||
for inclusion in `axum` by you, shall be licensed as MIT, without any
|
||||
additional terms or conditions.
|
||||
|
||||
[`axum`]: https://crates.io/crates/axum
|
||||
[docs]: https://docs.rs/axum-debug
|
||||
|
|
|
@ -1,10 +1,5 @@
|
|||
//! This is a debugging crate that provides better error messages for [`axum`] framework.
|
||||
//!
|
||||
//! [`axum`] is a great framework for developing web applications. But when you make a mistake,
|
||||
//! error messages can be really complex and long. It can take a long time for you to figure out
|
||||
//! what is wrong in your code. This crate provides utilities to generate better error messages in
|
||||
//! case you make a mistake.
|
||||
//!
|
||||
//! While using [`axum`], you can get long error messages for simple mistakes. For example:
|
||||
//!
|
||||
//! ```rust,compile_fail
|
||||
|
@ -84,13 +79,36 @@
|
|||
//!
|
||||
//! [`axum`]: axum
|
||||
//! [`Handler`]: axum::handler::Handler
|
||||
//! [`debug_handler`]: debug_handler
|
||||
//! [`debug_handler`]: macro@debug_handler
|
||||
|
||||
#![warn(
|
||||
clippy::all,
|
||||
clippy::dbg_macro,
|
||||
clippy::todo,
|
||||
clippy::empty_enum,
|
||||
clippy::enum_glob_use,
|
||||
clippy::mem_forget,
|
||||
clippy::unused_self,
|
||||
clippy::filter_map_next,
|
||||
clippy::needless_continue,
|
||||
clippy::needless_borrow,
|
||||
clippy::match_wildcard_for_single_variants,
|
||||
clippy::if_let_mutex,
|
||||
clippy::mismatched_target_os,
|
||||
clippy::await_holding_lock,
|
||||
clippy::match_on_vec_items,
|
||||
clippy::imprecise_flops,
|
||||
clippy::suboptimal_flops,
|
||||
clippy::lossy_float_literal,
|
||||
clippy::rest_pat_in_fully_bound_structs,
|
||||
clippy::fn_params_excessive_bools,
|
||||
clippy::exit,
|
||||
clippy::inefficient_to_string,
|
||||
clippy::linkedlist,
|
||||
clippy::macro_use_imports,
|
||||
clippy::option_option,
|
||||
clippy::verbose_file_reads,
|
||||
clippy::unnested_or_patterns,
|
||||
rust_2018_idioms,
|
||||
future_incompatible,
|
||||
nonstandard_style,
|
||||
|
@ -98,7 +116,294 @@
|
|||
missing_docs
|
||||
)]
|
||||
#![deny(unreachable_pub, private_in_public)]
|
||||
#![allow(elided_lifetimes_in_paths, clippy::type_complexity)]
|
||||
#![forbid(unsafe_code)]
|
||||
#![cfg_attr(docsrs, feature(doc_cfg))]
|
||||
#![cfg_attr(test, allow(clippy::float_cmp))]
|
||||
|
||||
pub use axum;
|
||||
pub use axum_debug_macros::debug_handler;
|
||||
use proc_macro::TokenStream;
|
||||
|
||||
/// Generates better error messages when applied to a handler function.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// Function is not async:
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// #[debug_handler]
|
||||
/// fn handler() -> &'static str {
|
||||
/// "Hello, world"
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ```text
|
||||
/// error: handlers must be async functions
|
||||
/// --> main.rs:xx:1
|
||||
/// |
|
||||
/// xx | fn handler() -> &'static str {
|
||||
/// | ^^
|
||||
/// ```
|
||||
///
|
||||
/// Wrong return type:
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// #[debug_handler]
|
||||
/// async fn handler() -> bool {
|
||||
/// false
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ```text
|
||||
/// error[E0277]: the trait bound `bool: IntoResponse` is not satisfied
|
||||
/// --> main.rs:xx:23
|
||||
/// |
|
||||
/// xx | async fn handler() -> bool {
|
||||
/// | ^^^^
|
||||
/// | |
|
||||
/// | the trait `IntoResponse` is not implemented for `bool`
|
||||
/// ```
|
||||
///
|
||||
/// Wrong extractor:
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// #[debug_handler]
|
||||
/// async fn handler(a: bool) -> String {
|
||||
/// format!("Can I extract a bool? {}", a)
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ```text
|
||||
/// error[E0277]: the trait bound `bool: FromRequest` is not satisfied
|
||||
/// --> main.rs:xx:21
|
||||
/// |
|
||||
/// xx | async fn handler(a: bool) -> String {
|
||||
/// | ^^^^
|
||||
/// | |
|
||||
/// | the trait `FromRequest` is not implemented for `bool`
|
||||
/// ```
|
||||
///
|
||||
/// Too many extractors:
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// #[debug_handler]
|
||||
/// async fn handler(
|
||||
/// a: String,
|
||||
/// b: String,
|
||||
/// c: String,
|
||||
/// d: String,
|
||||
/// e: String,
|
||||
/// f: String,
|
||||
/// g: String,
|
||||
/// h: String,
|
||||
/// i: String,
|
||||
/// j: String,
|
||||
/// k: String,
|
||||
/// l: String,
|
||||
/// m: String,
|
||||
/// n: String,
|
||||
/// o: String,
|
||||
/// p: String,
|
||||
/// q: String,
|
||||
/// ) {}
|
||||
/// ```
|
||||
///
|
||||
/// ```text
|
||||
/// error: too many extractors. 16 extractors are allowed
|
||||
/// note: you can nest extractors like "a: (Extractor, Extractor), b: (Extractor, Extractor)"
|
||||
/// --> main.rs:xx:5
|
||||
/// |
|
||||
/// xx | / a: String,
|
||||
/// xx | | b: String,
|
||||
/// xx | | c: String,
|
||||
/// xx | | d: String,
|
||||
/// ... |
|
||||
/// xx | | p: String,
|
||||
/// xx | | q: String,
|
||||
/// | |______________^
|
||||
/// ```
|
||||
///
|
||||
/// Future is not [`Send`]:
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// #[debug_handler]
|
||||
/// async fn handler() {
|
||||
/// let not_send = std::rc::Rc::new(());
|
||||
///
|
||||
/// async{}.await;
|
||||
/// }
|
||||
/// ```
|
||||
///
|
||||
/// ```text
|
||||
/// error: future cannot be sent between threads safely
|
||||
/// --> main.rs:xx:10
|
||||
/// |
|
||||
/// xx | async fn handler() {
|
||||
/// | ^^^^^^^
|
||||
/// | |
|
||||
/// | future returned by `handler` is not `Send`
|
||||
/// ```
|
||||
#[proc_macro_attribute]
|
||||
pub fn debug_handler(_attr: TokenStream, input: TokenStream) -> TokenStream {
|
||||
#[cfg(not(debug_assertions))]
|
||||
return input;
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
return debug::apply_debug_handler(input);
|
||||
}
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
mod debug {
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::Span;
|
||||
use quote::quote_spanned;
|
||||
use syn::{parse_macro_input, FnArg, Ident, ItemFn, ReturnType, Signature};
|
||||
|
||||
pub(crate) fn apply_debug_handler(input: TokenStream) -> TokenStream {
|
||||
let function = parse_macro_input!(input as ItemFn);
|
||||
|
||||
let vis = &function.vis;
|
||||
let sig = &function.sig;
|
||||
let ident = &sig.ident;
|
||||
let span = ident.span();
|
||||
let len = sig.inputs.len();
|
||||
let generics = create_generics(len);
|
||||
let params = sig.inputs.iter().map(|fn_arg| {
|
||||
if let FnArg::Typed(pat_type) = fn_arg {
|
||||
&pat_type.pat
|
||||
} else {
|
||||
panic!("not a handler function");
|
||||
}
|
||||
});
|
||||
let block = &function.block;
|
||||
|
||||
if let Err(error) = async_check(sig) {
|
||||
return error;
|
||||
}
|
||||
|
||||
if let Err(error) = param_limit_check(sig) {
|
||||
return error;
|
||||
}
|
||||
|
||||
let check_trait = check_trait_code(sig, &generics);
|
||||
let check_return = check_return_code(sig, &generics);
|
||||
let check_params = check_params_code(sig, &generics);
|
||||
|
||||
let expanded = quote_spanned! {span=>
|
||||
#vis #sig {
|
||||
#check_trait
|
||||
#check_return
|
||||
#(#check_params)*
|
||||
|
||||
#sig #block
|
||||
|
||||
#ident(#(#params),*).await
|
||||
}
|
||||
};
|
||||
|
||||
expanded.into()
|
||||
}
|
||||
|
||||
fn create_generics(len: usize) -> Vec<Ident> {
|
||||
let mut vec = Vec::new();
|
||||
for i in 1..=len {
|
||||
vec.push(Ident::new(&format!("T{}", i), Span::call_site()));
|
||||
}
|
||||
vec
|
||||
}
|
||||
|
||||
fn async_check(sig: &Signature) -> Result<(), TokenStream> {
|
||||
if sig.asyncness.is_none() {
|
||||
let error = syn::Error::new_spanned(sig.fn_token, "handlers must be async functions")
|
||||
.to_compile_error()
|
||||
.into();
|
||||
|
||||
return Err(error);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn param_limit_check(sig: &Signature) -> Result<(), TokenStream> {
|
||||
if sig.inputs.len() > 16 {
|
||||
let msg = "too many extractors. 16 extractors are allowed\n\
|
||||
note: you can nest extractors like \"a: (Extractor, Extractor), b: (Extractor, Extractor)\"";
|
||||
|
||||
let error = syn::Error::new_spanned(&sig.inputs, msg)
|
||||
.to_compile_error()
|
||||
.into();
|
||||
|
||||
return Err(error);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_trait_code(sig: &Signature, generics: &[Ident]) -> proc_macro2::TokenStream {
|
||||
let ident = &sig.ident;
|
||||
let span = ident.span();
|
||||
|
||||
quote_spanned! {span=>
|
||||
{
|
||||
debug_handler(#ident);
|
||||
|
||||
fn debug_handler<F, Fut, #(#generics),*>(_f: F)
|
||||
where
|
||||
F: ::std::ops::FnOnce(#(#generics),*) -> Fut + Clone + Send + Sync + 'static,
|
||||
Fut: ::std::future::Future + Send,
|
||||
{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_return_code(sig: &Signature, generics: &[Ident]) -> proc_macro2::TokenStream {
|
||||
let span = match &sig.output {
|
||||
ReturnType::Default => syn::Error::new_spanned(&sig.output, "").span(),
|
||||
ReturnType::Type(_, t) => syn::Error::new_spanned(t, "").span(),
|
||||
};
|
||||
let ident = &sig.ident;
|
||||
|
||||
quote_spanned! {span=>
|
||||
{
|
||||
debug_handler(#ident);
|
||||
|
||||
fn debug_handler<F, Fut, Res, #(#generics),*>(_f: F)
|
||||
where
|
||||
F: ::std::ops::FnOnce(#(#generics),*) -> Fut,
|
||||
Fut: ::std::future::Future<Output = Res>,
|
||||
Res: ::axum::response::IntoResponse,
|
||||
{}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_params_code(sig: &Signature, generics: &[Ident]) -> Vec<proc_macro2::TokenStream> {
|
||||
let mut vec = Vec::new();
|
||||
|
||||
let ident = &sig.ident;
|
||||
|
||||
for (i, generic) in generics.iter().enumerate() {
|
||||
let span = if let FnArg::Typed(pat_type) = &sig.inputs[i] {
|
||||
syn::Error::new_spanned(&pat_type.ty, "").span()
|
||||
} else {
|
||||
panic!("not a handler")
|
||||
};
|
||||
|
||||
let token_stream = quote_spanned! {span=>
|
||||
{
|
||||
debug_handler(#ident);
|
||||
|
||||
fn debug_handler<F, Fut, #(#generics),*>(_f: F)
|
||||
where
|
||||
F: ::std::ops::FnOnce(#(#generics),*) -> Fut,
|
||||
Fut: ::std::future::Future,
|
||||
#generic: ::axum::extract::FromRequest + Send,
|
||||
{}
|
||||
}
|
||||
};
|
||||
|
||||
vec.push(token_stream);
|
||||
}
|
||||
|
||||
vec
|
||||
}
|
||||
}
|
||||
|
|
|
@ -125,7 +125,6 @@
|
|||
clippy::option_option,
|
||||
clippy::verbose_file_reads,
|
||||
clippy::unnested_or_patterns,
|
||||
clippy::self_named_module_files,
|
||||
rust_2018_idioms,
|
||||
future_incompatible,
|
||||
nonstandard_style,
|
||||
|
|
|
@ -295,7 +295,6 @@
|
|||
clippy::option_option,
|
||||
clippy::verbose_file_reads,
|
||||
clippy::unnested_or_patterns,
|
||||
clippy::self_named_module_files,
|
||||
rust_2018_idioms,
|
||||
future_incompatible,
|
||||
nonstandard_style,
|
||||
|
|
Loading…
Add table
Reference in a new issue