diff options
| author | Tolmachev Igor <me@igorek.dev> | 2025-09-23 23:17:34 +0300 |
|---|---|---|
| committer | Tolmachev Igor <me@igorek.dev> | 2025-09-23 23:17:34 +0300 |
| commit | 552f1ffeb6f7ab9efc4382216751949f94709bea (patch) | |
| tree | ecf236f24e3e6af62fa2a8bd90300257ccdb185f | |
| parent | 4eebace8f1bcf002162c99c768695fd06d31090c (diff) | |
| download | queue_server-552f1ffeb6f7ab9efc4382216751949f94709bea.tar.gz queue_server-552f1ffeb6f7ab9efc4382216751949f94709bea.zip | |
Use IntoResponses derive macro
| -rw-r--r-- | src/response/mod.rs | 79 |
1 files changed, 19 insertions, 60 deletions
diff --git a/src/response/mod.rs b/src/response/mod.rs index 166bc13..d555596 100644 --- a/src/response/mod.rs +++ b/src/response/mod.rs | |||
| @@ -2,69 +2,28 @@ mod error; | |||
| 2 | mod success; | 2 | mod success; |
| 3 | 3 | ||
| 4 | pub use error::ErrorResponse; | 4 | pub use error::ErrorResponse; |
| 5 | use serde_json::json; | ||
| 6 | pub use success::SuccessResponse; | 5 | pub use success::SuccessResponse; |
| 7 | 6 | ||
| 8 | use std::collections::BTreeMap; | 7 | use utoipa::IntoResponses; |
| 9 | |||
| 10 | use utoipa::{ | ||
| 11 | IntoResponses, ToSchema, | ||
| 12 | openapi::{ | ||
| 13 | ContentBuilder, RefOr, ResponseBuilder, ResponsesBuilder, example::ExampleBuilder, | ||
| 14 | response::Response, schema::RefBuilder, | ||
| 15 | }, | ||
| 16 | }; | ||
| 17 | 8 | ||
| 18 | pub type ApiResult<T> = Result<SuccessResponse<T>, ErrorResponse>; | 9 | pub type ApiResult<T> = Result<SuccessResponse<T>, ErrorResponse>; |
| 19 | 10 | ||
| 20 | pub struct GlobalResponses; | 11 | #[derive(IntoResponses)] |
| 21 | 12 | pub enum GlobalResponses { | |
| 22 | impl IntoResponses for GlobalResponses { | 13 | #[response( |
| 23 | fn responses() -> BTreeMap<String, RefOr<Response>> { | 14 | status = 400, |
| 24 | ResponsesBuilder::new() | 15 | description = "General response for invalid request", |
| 25 | .response( | 16 | examples( |
| 26 | "400", | 17 | ("Fail" = (value = json!(ErrorResponse::fail("SomeFailKind", "some fail message")))) |
| 27 | ResponseBuilder::new() | 18 | ) |
| 28 | .content( | 19 | )] |
| 29 | "application/json", | 20 | Fail(ErrorResponse), |
| 30 | ContentBuilder::new() | 21 | #[response( |
| 31 | .schema(Some( | 22 | status = 500, |
| 32 | RefBuilder::new() | 23 | description = "General response when a server error occurs", |
| 33 | .ref_location_from_schema_name(ErrorResponse::name()), | 24 | examples( |
| 34 | )) | 25 | ("Error" = (value = json!(ErrorResponse::error("SomeErrorKind", "some error message")))) |
| 35 | .examples_from_iter([( | 26 | ) |
| 36 | "Fail", | 27 | )] |
| 37 | ExampleBuilder::new().value(Some(json!(ErrorResponse::fail( | 28 | Error(ErrorResponse), |
| 38 | "SomeFailKind", | ||
| 39 | "some fail message" | ||
| 40 | )))), | ||
| 41 | )]) | ||
| 42 | .build(), | ||
| 43 | ) | ||
| 44 | .description("General response for invalid request"), | ||
| 45 | ) | ||
| 46 | .response( | ||
| 47 | "500", | ||
| 48 | ResponseBuilder::new() | ||
| 49 | .content( | ||
| 50 | "application/json", | ||
| 51 | ContentBuilder::new() | ||
| 52 | .schema(Some( | ||
| 53 | RefBuilder::new() | ||
| 54 | .ref_location_from_schema_name(ErrorResponse::name()), | ||
| 55 | )) | ||
| 56 | .examples_from_iter([( | ||
| 57 | "Error", | ||
| 58 | ExampleBuilder::new().value(Some(json!(ErrorResponse::error( | ||
| 59 | "SomeErrorKind", | ||
| 60 | "some error message" | ||
| 61 | )))), | ||
| 62 | )]) | ||
| 63 | .build(), | ||
| 64 | ) | ||
| 65 | .description("General response when a server error occurs"), | ||
| 66 | ) | ||
| 67 | .build() | ||
| 68 | .into() | ||
| 69 | } | ||
| 70 | } | 29 | } |
