mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-29 00:00:18 +02:00
Move axum-debug into axum-macros (#724)
* Move axum-debug into axum-macros * fix ref to axum-macros in changelog * Apply suggestions from code review Co-authored-by: Jonas Platte <[email protected]> Co-authored-by: Jonas Platte <[email protected]>
This commit is contained in:
co-authored by
Jonas Platte
parent
b1283e9708
commit
f6fc5ed80c
@@ -0,0 +1,22 @@
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::{rejection::JsonRejection, FromRequest, Json},
|
||||
};
|
||||
use axum_macros::FromRequest;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(Deserialize, FromRequest)]
|
||||
#[from_request(via(Json))]
|
||||
struct Extractor {
|
||||
one: i32,
|
||||
two: String,
|
||||
three: bool,
|
||||
}
|
||||
|
||||
fn assert_from_request()
|
||||
where
|
||||
Extractor: FromRequest<Body, Rejection = JsonRejection>,
|
||||
{
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,51 @@
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::{FromRequest, TypedHeader, rejection::{TypedHeaderRejection, StringRejection}},
|
||||
headers::{self, UserAgent},
|
||||
};
|
||||
use axum_macros::FromRequest;
|
||||
use std::convert::Infallible;
|
||||
|
||||
#[derive(FromRequest)]
|
||||
struct Extractor {
|
||||
uri: axum::http::Uri,
|
||||
user_agent: TypedHeader<UserAgent>,
|
||||
content_type: TypedHeader<headers::ContentType>,
|
||||
etag: Option<TypedHeader<headers::ETag>>,
|
||||
host: Result<TypedHeader<headers::Host>, TypedHeaderRejection>,
|
||||
body: String,
|
||||
}
|
||||
|
||||
fn assert_from_request()
|
||||
where
|
||||
Extractor: FromRequest<Body, Rejection = ExtractorRejection>,
|
||||
{
|
||||
}
|
||||
|
||||
fn assert_rejection(rejection: ExtractorRejection)
|
||||
where
|
||||
ExtractorRejection: std::fmt::Debug + std::fmt::Display + std::error::Error,
|
||||
{
|
||||
match rejection {
|
||||
ExtractorRejection::Uri(inner) => {
|
||||
let _: Infallible = inner;
|
||||
}
|
||||
ExtractorRejection::Body(inner) => {
|
||||
let _: StringRejection = inner;
|
||||
}
|
||||
ExtractorRejection::UserAgent(inner) => {
|
||||
let _: TypedHeaderRejection = inner;
|
||||
}
|
||||
ExtractorRejection::ContentType(inner) => {
|
||||
let _: TypedHeaderRejection = inner;
|
||||
}
|
||||
ExtractorRejection::Etag(inner) => {
|
||||
let _: Infallible = inner;
|
||||
}
|
||||
ExtractorRejection::Host(inner) => {
|
||||
let _: Infallible = inner;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,58 @@
|
||||
use axum::{
|
||||
body::Body,
|
||||
extract::{
|
||||
rejection::{ExtensionRejection, TypedHeaderRejection},
|
||||
Extension, FromRequest, TypedHeader,
|
||||
},
|
||||
headers::{self, UserAgent},
|
||||
};
|
||||
use axum_macros::FromRequest;
|
||||
use std::convert::Infallible;
|
||||
|
||||
#[derive(FromRequest)]
|
||||
struct Extractor {
|
||||
#[from_request(via(Extension))]
|
||||
state: State,
|
||||
#[from_request(via(TypedHeader))]
|
||||
user_agent: UserAgent,
|
||||
#[from_request(via(TypedHeader))]
|
||||
content_type: headers::ContentType,
|
||||
#[from_request(via(TypedHeader))]
|
||||
etag: Option<headers::ETag>,
|
||||
#[from_request(via(TypedHeader))]
|
||||
host: Result<headers::Host, TypedHeaderRejection>,
|
||||
}
|
||||
|
||||
fn assert_from_request()
|
||||
where
|
||||
Extractor: FromRequest<Body, Rejection = ExtractorRejection>,
|
||||
{
|
||||
}
|
||||
|
||||
fn assert_rejection(rejection: ExtractorRejection)
|
||||
where
|
||||
ExtractorRejection: std::fmt::Debug + std::fmt::Display + std::error::Error,
|
||||
{
|
||||
match rejection {
|
||||
ExtractorRejection::State(inner) => {
|
||||
let _: ExtensionRejection = inner;
|
||||
}
|
||||
ExtractorRejection::UserAgent(inner) => {
|
||||
let _: TypedHeaderRejection = inner;
|
||||
}
|
||||
ExtractorRejection::ContentType(inner) => {
|
||||
let _: TypedHeaderRejection = inner;
|
||||
}
|
||||
ExtractorRejection::Etag(inner) => {
|
||||
let _: Infallible = inner;
|
||||
}
|
||||
ExtractorRejection::Host(inner) => {
|
||||
let _: Infallible = inner;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct State;
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,12 @@
|
||||
use axum_macros::FromRequest;
|
||||
|
||||
#[derive(FromRequest)]
|
||||
struct Extractor(axum::http::HeaderMap, String);
|
||||
|
||||
fn assert_from_request()
|
||||
where
|
||||
Extractor: axum::extract::FromRequest<axum::body::Body>,
|
||||
{
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,34 @@
|
||||
use axum::extract::{Query, rejection::*};
|
||||
use axum_macros::FromRequest;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(FromRequest)]
|
||||
struct Extractor(
|
||||
Query<Payload>,
|
||||
axum::extract::Json<Payload>,
|
||||
);
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Payload {}
|
||||
|
||||
fn assert_from_request()
|
||||
where
|
||||
Extractor: axum::extract::FromRequest<axum::body::Body>,
|
||||
{
|
||||
}
|
||||
|
||||
fn assert_rejection(rejection: ExtractorRejection)
|
||||
where
|
||||
ExtractorRejection: std::fmt::Debug + std::fmt::Display + std::error::Error,
|
||||
{
|
||||
match rejection {
|
||||
ExtractorRejection::QueryPayload(inner) => {
|
||||
let _: QueryRejection = inner;
|
||||
}
|
||||
ExtractorRejection::JsonPayload(inner) => {
|
||||
let _: JsonRejection = inner;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,34 @@
|
||||
use axum::extract::{Query, rejection::*};
|
||||
use axum_macros::FromRequest;
|
||||
use serde::Deserialize;
|
||||
|
||||
#[derive(FromRequest)]
|
||||
struct Extractor(
|
||||
#[from_request(via(Query))] Payload,
|
||||
#[from_request(via(axum::extract::Json))] Payload,
|
||||
);
|
||||
|
||||
fn assert_rejection(rejection: ExtractorRejection)
|
||||
where
|
||||
ExtractorRejection: std::fmt::Debug + std::fmt::Display + std::error::Error,
|
||||
{
|
||||
match rejection {
|
||||
ExtractorRejection::QueryPayload(inner) => {
|
||||
let _: QueryRejection = inner;
|
||||
}
|
||||
ExtractorRejection::JsonPayload(inner) => {
|
||||
let _: JsonRejection = inner;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Payload {}
|
||||
|
||||
fn assert_from_request()
|
||||
where
|
||||
Extractor: axum::extract::FromRequest<axum::body::Body>,
|
||||
{
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,16 @@
|
||||
use axum_macros::FromRequest;
|
||||
use axum::extract::Extension;
|
||||
|
||||
#[derive(FromRequest)]
|
||||
struct Extractor(#[from_request(via(Extension))] State);
|
||||
|
||||
#[derive(Clone)]
|
||||
struct State;
|
||||
|
||||
fn assert_from_request()
|
||||
where
|
||||
Extractor: axum::extract::FromRequest<axum::body::Body>,
|
||||
{
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,12 @@
|
||||
use axum_macros::FromRequest;
|
||||
|
||||
#[derive(FromRequest)]
|
||||
struct Extractor;
|
||||
|
||||
fn assert_from_request()
|
||||
where
|
||||
Extractor: axum::extract::FromRequest<axum::body::Body>,
|
||||
{
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
Reference in New Issue
Block a user