aboutsummaryrefslogtreecommitdiff
path: root/src/response/mod.rs
diff options
context:
space:
mode:
authorTolmachev Igor <me@igorek.dev>2025-09-23 23:17:34 +0300
committerTolmachev Igor <me@igorek.dev>2025-09-23 23:17:34 +0300
commit552f1ffeb6f7ab9efc4382216751949f94709bea (patch)
treeecf236f24e3e6af62fa2a8bd90300257ccdb185f /src/response/mod.rs
parent4eebace8f1bcf002162c99c768695fd06d31090c (diff)
downloadqueue_server-552f1ffeb6f7ab9efc4382216751949f94709bea.tar.gz
queue_server-552f1ffeb6f7ab9efc4382216751949f94709bea.zip
Use IntoResponses derive macro
Diffstat (limited to 'src/response/mod.rs')
-rw-r--r--src/response/mod.rs79
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;
2mod success; 2mod success;
3 3
4pub use error::ErrorResponse; 4pub use error::ErrorResponse;
5use serde_json::json;
6pub use success::SuccessResponse; 5pub use success::SuccessResponse;
7 6
8use std::collections::BTreeMap; 7use utoipa::IntoResponses;
9
10use utoipa::{
11 IntoResponses, ToSchema,
12 openapi::{
13 ContentBuilder, RefOr, ResponseBuilder, ResponsesBuilder, example::ExampleBuilder,
14 response::Response, schema::RefBuilder,
15 },
16};
17 8
18pub type ApiResult<T> = Result<SuccessResponse<T>, ErrorResponse>; 9pub type ApiResult<T> = Result<SuccessResponse<T>, ErrorResponse>;
19 10
20pub struct GlobalResponses; 11#[derive(IntoResponses)]
21 12pub enum GlobalResponses {
22impl 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}