1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//! axum の extractor を薄く包み、失敗時も `AppError`(JSON)で返すようにする。
//! ハンドラ側は axum 標準のものと同じ書き味で使える。
use axum::extract::{FromRequest, FromRequestParts};
use axum::response::{IntoResponse, Response};
use crate::error::AppError;
use serde::Serialize;
/// リクエストボディの JSON。レスポンスにもそのまま使える
#[derive(FromRequest)]
#[from_request(via(axum::Json), rejection(AppError))]
pub struct Json<T>(pub T);
impl<T: Serialize> IntoResponse for Json<T> {
fn into_response(self) -> Response {
axum::Json(self.0).into_response()
}
}
/// パスパラメータ(例: `/todos/{id}`)
#[derive(FromRequestParts)]
#[from_request(via(axum::extract::Path), rejection(AppError))]
pub struct Path<T>(pub T);
/// クエリ文字列(例: `?done=true&limit=10`)
#[derive(FromRequestParts)]
#[from_request(via(axum::extract::Query), rejection(AppError))]
pub struct Query<T>(pub T);