diff options
| author | Tolmachev Igor <me@igorek.dev> | 2025-09-26 20:46:02 +0300 |
|---|---|---|
| committer | Tolmachev Igor <me@igorek.dev> | 2025-09-26 20:46:02 +0300 |
| commit | 256d287ba68a83a04ec24f6171a72a64a77f9093 (patch) | |
| tree | beddf66bdcd2973a4f1d00b41cd8875765c392b4 | |
| parent | a5fade79518cce7031d89b04112d9efe9361d961 (diff) | |
| download | queue_server-256d287ba68a83a04ec24f6171a72a64a77f9093.tar.gz queue_server-256d287ba68a83a04ec24f6171a72a64a77f9093.zip | |
Add Query extractor
| -rw-r--r-- | src/error/client.rs | 3 | ||||
| -rw-r--r-- | src/error/mod.rs | 8 | ||||
| -rw-r--r-- | src/extract/mod.rs | 2 | ||||
| -rw-r--r-- | src/extract/query.rs | 21 |
4 files changed, 33 insertions, 1 deletions
diff --git a/src/error/client.rs b/src/error/client.rs index 07d5f97..e93bbb7 100644 --- a/src/error/client.rs +++ b/src/error/client.rs | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | pub enum ClientError { | 1 | pub enum ClientError { |
| 2 | BadJsonBody(String), | 2 | BadJsonBody(String), |
| 3 | BadQueryString(String), | ||
| 3 | BadAuthTokenHeader(String), | 4 | BadAuthTokenHeader(String), |
| 4 | UsernameIsTaken { username: String }, | 5 | UsernameIsTaken { username: String }, |
| 5 | InvalidPassword, | 6 | InvalidPassword, |
| @@ -13,6 +14,7 @@ impl ClientError { | |||
| 13 | pub fn kind(&self) -> String { | 14 | pub fn kind(&self) -> String { |
| 14 | match self { | 15 | match self { |
| 15 | Self::BadJsonBody(..) => "BadJsonBody", | 16 | Self::BadJsonBody(..) => "BadJsonBody", |
| 17 | Self::BadQueryString(..) => "BadQueryString", | ||
| 16 | Self::BadAuthTokenHeader(..) => "BadAuthTokenHeader", | 18 | Self::BadAuthTokenHeader(..) => "BadAuthTokenHeader", |
| 17 | Self::UsernameIsTaken { .. } => "UsernameIsTaken", | 19 | Self::UsernameIsTaken { .. } => "UsernameIsTaken", |
| 18 | Self::InvalidPassword => "InvalidPassword", | 20 | Self::InvalidPassword => "InvalidPassword", |
| @@ -27,6 +29,7 @@ impl ClientError { | |||
| 27 | pub fn into_message(self) -> String { | 29 | pub fn into_message(self) -> String { |
| 28 | match self { | 30 | match self { |
| 29 | Self::BadJsonBody(msg) => msg, | 31 | Self::BadJsonBody(msg) => msg, |
| 32 | Self::BadQueryString(msg) => msg, | ||
| 30 | Self::BadAuthTokenHeader(msg) => msg, | 33 | Self::BadAuthTokenHeader(msg) => msg, |
| 31 | Self::UsernameIsTaken { username } => { | 34 | Self::UsernameIsTaken { username } => { |
| 32 | format!("username `{}` is taken", username) | 35 | format!("username `{}` is taken", username) |
diff --git a/src/error/mod.rs b/src/error/mod.rs index 4b8e1c4..07db26f 100644 --- a/src/error/mod.rs +++ b/src/error/mod.rs | |||
| @@ -5,7 +5,7 @@ pub use client::ClientError; | |||
| 5 | pub use server::ServerError; | 5 | pub use server::ServerError; |
| 6 | 6 | ||
| 7 | use argon2::password_hash::Error as PasswordHashError; | 7 | use argon2::password_hash::Error as PasswordHashError; |
| 8 | use axum::extract::rejection::JsonRejection; | 8 | use axum::extract::rejection::{JsonRejection, QueryRejection}; |
| 9 | use axum_extra::typed_header::TypedHeaderRejection; | 9 | use axum_extra::typed_header::TypedHeaderRejection; |
| 10 | use sea_orm::DbErr; | 10 | use sea_orm::DbErr; |
| 11 | 11 | ||
| @@ -34,6 +34,12 @@ impl From<JsonRejection> for ApiError { | |||
| 34 | } | 34 | } |
| 35 | } | 35 | } |
| 36 | 36 | ||
| 37 | impl From<QueryRejection> for ApiError { | ||
| 38 | fn from(value: QueryRejection) -> Self { | ||
| 39 | ClientError::BadJsonBody(value.body_text()).into() | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 37 | impl From<TypedHeaderRejection> for ApiError { | 43 | impl From<TypedHeaderRejection> for ApiError { |
| 38 | fn from(value: TypedHeaderRejection) -> Self { | 44 | fn from(value: TypedHeaderRejection) -> Self { |
| 39 | ClientError::BadAuthTokenHeader(value.to_string()).into() | 45 | ClientError::BadAuthTokenHeader(value.to_string()).into() |
diff --git a/src/extract/mod.rs b/src/extract/mod.rs index b46a610..e16b9d4 100644 --- a/src/extract/mod.rs +++ b/src/extract/mod.rs | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | mod auth; | 1 | mod auth; |
| 2 | mod json; | 2 | mod json; |
| 3 | mod query; | ||
| 3 | 4 | ||
| 4 | pub use auth::Auth; | 5 | pub use auth::Auth; |
| 5 | pub use json::ApiJson; | 6 | pub use json::ApiJson; |
| 7 | pub use query::ApiQuery; | ||
diff --git a/src/extract/query.rs b/src/extract/query.rs new file mode 100644 index 0000000..53c8993 --- /dev/null +++ b/src/extract/query.rs | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | use axum::{ | ||
| 2 | extract::{FromRequestParts, Query, rejection::QueryRejection}, | ||
| 3 | http::request::Parts, | ||
| 4 | }; | ||
| 5 | |||
| 6 | use crate::ErrorResponse; | ||
| 7 | |||
| 8 | pub struct ApiQuery<T>(pub T); | ||
| 9 | |||
| 10 | impl<S, T> FromRequestParts<S> for ApiQuery<T> | ||
| 11 | where | ||
| 12 | Query<T>: FromRequestParts<S, Rejection = QueryRejection>, | ||
| 13 | S: Send + Sync, | ||
| 14 | { | ||
| 15 | type Rejection = ErrorResponse; | ||
| 16 | |||
| 17 | #[inline] | ||
| 18 | async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> { | ||
| 19 | Ok(Self(Query::from_request_parts(parts, state).await?.0)) | ||
| 20 | } | ||
| 21 | } | ||
