wae_schema/openapi/
builder.rs1use super::super::*;
6use std::collections::BTreeMap;
7
8pub struct OpenApiBuilder {
12 doc: OpenApiDoc,
13}
14
15impl OpenApiBuilder {
16 pub fn new() -> Self {
18 Self { doc: OpenApiDoc::default() }
19 }
20
21 pub fn with_title_and_version(title: impl Into<String>, version: impl Into<String>) -> Self {
23 Self { doc: OpenApiDoc::new(title, version) }
24 }
25
26 pub fn title(mut self, title: impl Into<String>) -> Self {
28 self.doc.info.title = title.into();
29 self
30 }
31
32 pub fn version(mut self, version: impl Into<String>) -> Self {
34 self.doc.info.version = version.into();
35 self
36 }
37
38 pub fn description(mut self, desc: impl Into<String>) -> Self {
40 self.doc.info.description = Some(desc.into());
41 self
42 }
43
44 pub fn terms_of_service(mut self, url: impl Into<String>) -> Self {
46 self.doc.info.terms_of_service = Some(url.into());
47 self
48 }
49
50 pub fn contact(mut self, contact: Contact) -> Self {
52 self.doc.info.contact = Some(contact);
53 self
54 }
55
56 pub fn license(mut self, license: License) -> Self {
58 self.doc.info.license = Some(license);
59 self
60 }
61
62 pub fn path(mut self, path: impl Into<String>, item: PathItem) -> Self {
64 self.doc.paths.insert(path.into(), item);
65 self
66 }
67
68 pub fn paths(mut self, paths: BTreeMap<String, PathItem>) -> Self {
70 self.doc.paths.extend(paths);
71 self
72 }
73
74 pub fn schema(mut self, name: impl Into<String>, schema: Schema) -> Self {
76 let components = self.doc.components.get_or_insert_with(Components::default);
77 components.schemas.insert(name.into(), schema);
78 self
79 }
80
81 pub fn schemas(mut self, schemas: BTreeMap<String, Schema>) -> Self {
83 let components = self.doc.components.get_or_insert_with(Components::default);
84 components.schemas.extend(schemas);
85 self
86 }
87
88 pub fn response(mut self, name: impl Into<String>, response: Response) -> Self {
90 let components = self.doc.components.get_or_insert_with(Components::default);
91 components.responses.insert(name.into(), response);
92 self
93 }
94
95 pub fn responses(mut self, responses: BTreeMap<String, Response>) -> Self {
97 let components = self.doc.components.get_or_insert_with(Components::default);
98 components.responses.extend(responses);
99 self
100 }
101
102 pub fn parameter(mut self, name: impl Into<String>, parameter: Parameter) -> Self {
104 let components = self.doc.components.get_or_insert_with(Components::default);
105 components.parameters.insert(name.into(), parameter);
106 self
107 }
108
109 pub fn parameters(mut self, parameters: BTreeMap<String, Parameter>) -> Self {
111 let components = self.doc.components.get_or_insert_with(Components::default);
112 components.parameters.extend(parameters);
113 self
114 }
115
116 pub fn request_body(mut self, name: impl Into<String>, body: RequestBody) -> Self {
118 let components = self.doc.components.get_or_insert_with(Components::default);
119 components.request_bodies.insert(name.into(), body);
120 self
121 }
122
123 pub fn request_bodies(mut self, bodies: BTreeMap<String, RequestBody>) -> Self {
125 let components = self.doc.components.get_or_insert_with(Components::default);
126 components.request_bodies.extend(bodies);
127 self
128 }
129
130 pub fn security_scheme(mut self, name: impl Into<String>, scheme: SecurityScheme) -> Self {
132 let components = self.doc.components.get_or_insert_with(Components::default);
133 components.security_schemes.insert(name.into(), scheme);
134 self
135 }
136
137 pub fn security_schemes(mut self, schemes: BTreeMap<String, SecurityScheme>) -> Self {
139 let components = self.doc.components.get_or_insert_with(Components::default);
140 components.security_schemes.extend(schemes);
141 self
142 }
143
144 pub fn server(mut self, url: impl Into<String>, description: Option<String>) -> Self {
146 let servers = self.doc.servers.get_or_insert_with(Vec::new);
147 servers.push(Server { url: url.into(), description, variables: None });
148 self
149 }
150
151 pub fn servers(mut self, servers: Vec<Server>) -> Self {
153 let doc_servers = self.doc.servers.get_or_insert_with(Vec::new);
154 doc_servers.extend(servers);
155 self
156 }
157
158 pub fn tag(mut self, name: impl Into<String>, description: Option<String>) -> Self {
160 let tags = self.doc.tags.get_or_insert_with(Vec::new);
161 tags.push(Tag { name: name.into(), description, external_docs: None });
162 self
163 }
164
165 pub fn tags(mut self, tags: Vec<Tag>) -> Self {
167 let doc_tags = self.doc.tags.get_or_insert_with(Vec::new);
168 doc_tags.extend(tags);
169 self
170 }
171
172 pub fn external_docs(mut self, url: impl Into<String>, description: Option<String>) -> Self {
174 self.doc.external_docs = Some(ExternalDocumentation { url: url.into(), description });
175 self
176 }
177
178 pub fn security(mut self, security: SecurityRequirement) -> Self {
180 let securities = self.doc.security.get_or_insert_with(Vec::new);
181 securities.push(security);
182 self
183 }
184
185 pub fn securities(mut self, securities: Vec<SecurityRequirement>) -> Self {
187 let doc_securities = self.doc.security.get_or_insert_with(Vec::new);
188 doc_securities.extend(securities);
189 self
190 }
191
192 pub fn build(self) -> OpenApiDoc {
194 self.doc
195 }
196}
197
198impl Default for OpenApiBuilder {
199 fn default() -> Self {
201 Self::new()
202 }
203}