Move `axum-debug` crate to workspace (#497)

* add axum-debug to workspace

* update readme

* add changes to changelog

* little docs update

* fix the gap

a tab has leaked into workspace Cargo.toml, it must be fixed

* address clippy warnings
This commit is contained in:
Eray Karatay 2021-11-11 17:18:40 +03:00 committed by GitHub
parent 2507463706
commit f6b47478da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 575 additions and 0 deletions

View File

@ -1,6 +1,8 @@
[workspace] [workspace]
members = [ members = [
"axum", "axum",
"axum-debug",
"axum-debug-macros",
"axum-handle-error-extract", "axum-handle-error-extract",
"examples/*", "examples/*",
] ]

View File

@ -0,0 +1,13 @@
# 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.

View File

@ -0,0 +1,20 @@
[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"] }

View File

@ -0,0 +1,7 @@
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.

View File

@ -0,0 +1,23 @@
[![License](https://img.shields.io/crates/l/axum-debug-macros)](https://choosealicense.com/licenses/mit/)
[![Crates.io](https://img.shields.io/crates/v/axum-debug-macros)](https://crates.io/crates/axum-debug-macros)
[![Docs - Stable](https://img.shields.io/crates/v/axum-debug-macros?color=blue&label=docs)](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

View File

@ -0,0 +1,305 @@
//! 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
}
}

15
axum-debug/CHANGELOG.md Normal file
View File

@ -0,0 +1,15 @@
# 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.
- **breaking:** Removed `check_service` function.
- **breaking:** Removed `debug_service` function.
# 0.1.0 (6. October 2021)
- Initial release.

16
axum-debug/Cargo.toml Normal file
View File

@ -0,0 +1,16 @@
[package]
authors = ["Programatik <programatik29@gmail.com>"]
categories = ["development-tools::debugging"]
description = "Better error messages for axum framework."
edition = "2018"
homepage = "https://github.com/tokio-rs/axum"
keywords = ["axum", "debugging", "debug"]
license = "MIT"
name = "axum-debug"
readme = "README.md"
repository = "https://github.com/tokio-rs/axum"
version = "0.1.0"
[dependencies]
axum = { path = "../axum" }
axum-debug-macros = { path = "../axum-debug-macros" }

7
axum-debug/LICENSE Normal file
View File

@ -0,0 +1,7 @@
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.

63
axum-debug/README.md Normal file
View File

@ -0,0 +1,63 @@
[![License](https://img.shields.io/crates/l/axum-debug)](https://choosealicense.com/licenses/mit/)
[![Crates.io](https://img.shields.io/crates/v/axum-debug)](https://crates.io/crates/axum-debug)
[![Docs - Stable](https://img.shields.io/crates/v/axum-debug?color=blue&label=docs)](https://docs.rs/axum-debug/)
# 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`
```
## Safety
This crate uses `#![forbid(unsafe_code)]` to ensure everything is implemented in 100% safe Rust.
## Performance
Macros in this crate have no effect when using release profile. (eg. `cargo build --release`)
## License
This project is licensed under the [MIT license](LICENSE).
[`axum`]: https://crates.io/crates/axum

104
axum-debug/src/lib.rs Normal file
View File

@ -0,0 +1,104 @@
//! 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
//! use axum::{routing::get, Router};
//!
//! #[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();
//! }
//!
//! fn handler() -> &'static str {
//! "Hello, world"
//! }
//! ```
//!
//! You will get a long error message about function not implementing [`Handler`] trait. But why
//! this function does not implement it? To figure it out [`debug_handler`] macro can be used.
//!
//! ```rust,compile_fail
//! # 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]
//! fn handler() -> &'static str {
//! "Hello, world"
//! }
//! ```
//!
//! ```text
//! error: handlers must be async functions
//! --> main.rs:xx:1
//! |
//! xx | fn handler() -> &'static str {
//! | ^^
//! ```
//!
//! As the error message says, handler function needs to be async.
//!
//! ```rust,compile_fail
//! 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() -> &'static str {
//! "Hello, world"
//! }
//! ```
//!
//! # Performance
//!
//! Macros in this crate have no effect when using release profile. (eg. `cargo build --release`)
//!
//! [`axum`]: axum
//! [`Handler`]: axum::handler::Handler
//! [`debug_handler`]: debug_handler
#![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)]
pub use axum;
pub use axum_debug_macros::debug_handler;