ycdemo / axum / axum-macros/ tests/ from_request/ pass/ option_via_json.rs

use axum::{
    extract::rejection::{JsonRejection, PathRejection},
    response::{IntoResponse, Response},
    routing::post,
    Json, Router,
};
use axum_macros::FromRequest;
use serde::Deserialize;

fn main() {
    let _: Router = Router::new().route("/{something}", post(handler));
}

async fn handler(_: Args) {}

#[derive(Deserialize)]
struct Payload {
    value: String,
}

#[derive(FromRequest)]
#[from_request(rejection(MyError))]
struct Args {
    #[from_request(via(axum::extract::Path))]
    something: String,
    #[from_request(via(Json))]
    request: Option<Payload>,
}

struct MyError(Response);

impl From<PathRejection> for MyError {
    fn from(rejection: PathRejection) -> Self {
        Self(rejection.into_response())
    }
}

impl From<JsonRejection> for MyError {
    fn from(rejection: JsonRejection) -> Self {
        Self(rejection.into_response())
    }
}

impl IntoResponse for MyError {
    fn into_response(self) -> Response {
        self.0
    }
}