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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
mod error;
mod success;
pub use error::ErrorResponse;
use serde_json::json;
pub use success::SuccessResponse;
use std::collections::BTreeMap;
use utoipa::{
IntoResponses, ToSchema,
openapi::{
ContentBuilder, RefOr, ResponseBuilder, ResponsesBuilder, example::ExampleBuilder,
response::Response, schema::RefBuilder,
},
};
pub type ApiResult<T> = Result<SuccessResponse<T>, ErrorResponse>;
pub struct GlobalResponses;
impl IntoResponses for GlobalResponses {
fn responses() -> BTreeMap<String, RefOr<Response>> {
ResponsesBuilder::new()
.response(
"400",
ResponseBuilder::new()
.content(
"application/json",
ContentBuilder::new()
.schema(Some(
RefBuilder::new()
.ref_location_from_schema_name(ErrorResponse::name()),
))
.examples_from_iter([(
"Fail",
ExampleBuilder::new().value(Some(json!(ErrorResponse::fail(
"SomeFailKind",
"some fail message"
)))),
)])
.build(),
)
.description("General response for invalid request"),
)
.response(
"500",
ResponseBuilder::new()
.content(
"application/json",
ContentBuilder::new()
.schema(Some(
RefBuilder::new()
.ref_location_from_schema_name(ErrorResponse::name()),
))
.examples_from_iter([(
"Error",
ExampleBuilder::new().value(Some(json!(ErrorResponse::error(
"SomeErrorKind",
"some error message"
)))),
)])
.build(),
)
.description("General response when a server error occurs"),
)
.build()
.into()
}
}
|