2022-08-12 18:05:27 +02:00
|
|
|
use axum::{
|
|
|
|
async_trait,
|
2023-03-20 21:02:40 +01:00
|
|
|
extract::{Request, rejection::ExtensionRejection, FromRequest},
|
|
|
|
http::StatusCode,
|
2022-08-12 18:05:27 +02:00
|
|
|
response::{IntoResponse, Response},
|
|
|
|
routing::get,
|
2023-11-23 12:03:03 +01:00
|
|
|
body::Body,
|
2022-08-12 18:05:27 +02:00
|
|
|
Extension, Router,
|
|
|
|
};
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let _: Router = Router::new().route("/", get(handler).post(handler_result));
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn handler(_: MyExtractor) {}
|
|
|
|
|
|
|
|
async fn handler_result(_: Result<MyExtractor, MyRejection>) {}
|
|
|
|
|
|
|
|
#[derive(FromRequest)]
|
|
|
|
#[from_request(rejection(MyRejection))]
|
|
|
|
struct MyExtractor {
|
|
|
|
one: Extension<String>,
|
|
|
|
#[from_request(via(Extension))]
|
|
|
|
two: String,
|
|
|
|
three: OtherExtractor,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct OtherExtractor;
|
|
|
|
|
|
|
|
#[async_trait]
|
2023-03-12 16:37:32 +01:00
|
|
|
impl<S> FromRequest<S> for OtherExtractor
|
2022-08-12 18:05:27 +02:00
|
|
|
where
|
2022-08-17 22:08:24 +02:00
|
|
|
S: Send + Sync,
|
2022-08-12 18:05:27 +02:00
|
|
|
{
|
|
|
|
// this rejection doesn't implement `Display` and `Error`
|
|
|
|
type Rejection = (StatusCode, String);
|
|
|
|
|
2023-03-20 21:02:40 +01:00
|
|
|
async fn from_request(_req: Request, _state: &S) -> Result<Self, Self::Rejection> {
|
2022-08-12 18:05:27 +02:00
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct MyRejection {}
|
|
|
|
|
|
|
|
impl From<ExtensionRejection> for MyRejection {
|
|
|
|
fn from(_: ExtensionRejection) -> Self {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<(StatusCode, String)> for MyRejection {
|
|
|
|
fn from(_: (StatusCode, String)) -> Self {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoResponse for MyRejection {
|
|
|
|
fn into_response(self) -> Response {
|
|
|
|
todo!()
|
|
|
|
}
|
|
|
|
}
|