-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathdocument.rs
More file actions
132 lines (117 loc) · 3.99 KB
/
document.rs
File metadata and controls
132 lines (117 loc) · 3.99 KB
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
use super::{paths::{Paths, Operations}, schema::RawSchema, security::SecurityScheme, _util::Map};
use serde::Serialize;
#[derive(Serialize)]
pub struct Document {
openapi: &'static str,
info: Info,
servers: Vec<Server>,
paths: Paths,
#[serde(skip_serializing_if = "Components::is_empty")]
components: Components,
}
#[derive(Serialize)]
struct Info {
title: &'static str,
version: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<&'static str>,
}
#[derive(Serialize, Clone)]
pub struct Server {
url: &'static str,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<&'static str>,
#[serde(skip_serializing_if = "Option::is_none")]
variables: Option<Box<Map<&'static str, ServerVariable>>>
}
#[derive(Serialize, Clone)]
struct ServerVariable {
default: &'static str,
#[serde(rename = "enum")]
#[serde(skip_serializing_if = "Vec::is_empty")]
enumerates: Vec<&'static str>,
#[serde(skip_serializing_if = "Option::is_none")]
description: Option<&'static str>,
}
impl Server {
pub fn at(url: &'static str) -> Self {
Self { url, description:None, variables:None }
}
pub fn description(mut self, description: &'static str) -> Self {
self.description = Some(description);
self
}
pub fn var<const N: usize>(
mut self,
name: &'static str,
default: &'static str,
candidates: [&'static str; N]
) -> Self {
if self.variables.is_none() {
self.variables = Some(Box::new(Map::new()))
}
self.variables.as_mut().unwrap().insert(
name,
ServerVariable {
default,
enumerates: candidates.into(),
description: None
});
self
}
}
#[derive(Serialize, Clone)]
pub struct Components {
#[serde(skip_serializing_if = "Map::is_empty")]
schemas: Map<&'static str, RawSchema>,
#[serde(skip_serializing_if = "Map::is_empty")]
#[serde(rename = "securitySchemes")]
security_schemes: Map<&'static str, SecurityScheme>,
}
impl Components {
fn is_empty(&self) -> bool {
self.schemas.is_empty() && self.security_schemes.is_empty()
}
}
impl Document {
pub fn new(
title: &'static str,
version: &'static str,
servers: impl Into<Vec<Server>>
) -> Self {
Self {
openapi: "3.1.0",
info: Info { title, version, description:None },
servers: servers.into(),
paths: Paths::new(),
components: Components { schemas:Map::new(), security_schemes:Map::new() }
}
}
pub fn description(mut self, description: &'static str) -> Self {
self.info.description = Some(description);
self
}
pub fn path(mut self, path: impl Into<String>, operations: Operations) -> Self {
self.paths = self.paths.at(path, operations);
self
}
#[doc(hidden)]
pub fn register_schema_component(&mut self, schema: impl Into<RawSchema>) {
let schema: RawSchema = schema.into();
if let Some(name) = schema.__name__ {
match self.components.schemas.get(&name) {
Some(it) if *it == schema => return,
Some(_) => panic!("[OpenAPI] `components.schemas`: contradict registrations of multiple `{name}`s"),
None => self.components.schemas.insert(name, schema),
}
}
}
#[doc(hidden)]
pub fn register_securityScheme_component(&mut self, securityScheme: SecurityScheme) {
match self.components.security_schemes.get(&securityScheme.__name__) {
Some(it) if *it == securityScheme => return,
Some(_) => panic!("[OpenAPI] `components.security_schemes`: contradict registrations of multiple `{}`s", securityScheme.__name__),
None => self.components.security_schemes.insert(securityScheme.__name__, securityScheme),
}
}
}