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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::handlers::{ContentHandler, RedirectHandler};
use serde::{Deserialize, Serialize};
use rocket::http::ContentType;
use rocket::Route;
macro_rules! static_file {
($name: literal, $type: ident) => {
ContentHandler::bytes(
ContentType::$type,
include_bytes!(concat!("../swagger-ui/", $name)),
)
.into_route(concat!("/", $name))
};
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum DefaultModelRendering {
Example,
Model,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub enum DocExpansion {
List,
Full,
None,
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(untagged)]
pub enum Filter {
Bool(bool),
Str(String),
}
fn is_zero(num: &u32) -> bool {
*num == 0
}
#[derive(Debug, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SwaggerUIConfig {
#[serde(default, skip_serializing_if = "String::is_empty")]
pub url: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub urls: Vec<UrlObject>,
pub deep_linking: bool,
pub display_operation_id: bool,
pub default_models_expand_depth: i32,
pub default_model_expand_depth: i32,
pub default_model_rendering: DefaultModelRendering,
pub display_request_duration: bool,
pub doc_expansion: DocExpansion,
pub filter: Filter,
#[serde(default, skip_serializing_if = "is_zero")]
pub max_displayed_tags: u32,
pub show_extensions: bool,
pub show_common_extensions: bool,
}
impl Default for SwaggerUIConfig {
fn default() -> Self {
Self {
url: String::new(),
urls: vec![],
deep_linking: false,
display_operation_id: false,
default_model_expand_depth: 1,
default_model_rendering: DefaultModelRendering::Example,
default_models_expand_depth: 1,
display_request_duration: false,
doc_expansion: DocExpansion::List,
filter: Filter::Bool(false),
max_displayed_tags: 0,
show_extensions: false,
show_common_extensions: false,
}
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct UrlObject {
pub name: String,
pub url: String,
}
impl UrlObject {
pub fn new(name: &str, url: &str) -> Self {
Self {
name: name.to_string(),
url: url.to_string(),
}
}
}
pub fn make_swagger_ui(config: &SwaggerUIConfig) -> impl Into<Vec<Route>> {
let config_handler = ContentHandler::json(config);
vec![
config_handler.into_route("/swagger-ui-config.json"),
RedirectHandler::to("index.html").into_route("/"),
static_file!("index.html", HTML),
static_file!("oauth2-redirect.html", HTML),
static_file!("swagger-ui.js", JavaScript),
static_file!("swagger-ui-standalone-preset.js", JavaScript),
static_file!("swagger-ui-bundle.js", JavaScript),
static_file!("swagger-ui.css", CSS),
]
}