This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
mod.rs
404 lines (345 loc) · 12.5 KB
/
mod.rs
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//! # Highway
//!
//! This module is a database of components and paths
//! Useful when converting OpenAPI structures to printer structures
use crate::printer;
use indexmap::IndexMap;
use openapiv3::{ReferenceOr, Schema};
/// List of components ready to be printed
#[derive(Debug, Default)]
pub struct Components {
pub request_bodies: IndexMap<String, RequestBody>,
pub schemas: IndexMap<String, Component>,
}
#[derive(Debug, Default, PartialEq, Eq)]
pub struct RequestBody {
pub name: String,
pub required: bool,
pub description: Option<String>,
pub fields: IndexMap<String, ComponentField>,
}
#[derive(Debug, Default, PartialEq, Eq)]
pub struct Component {
pub name: String,
pub description: Option<String>,
pub kind: ComponentKind,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ComponentKind {
Object {
fields: IndexMap<String, ComponentField>,
},
Array {
items: FieldType,
},
Integer,
String,
Number,
Boolean,
}
impl Default for ComponentKind {
fn default() -> Self {
Self::String
}
}
#[derive(Debug, Default, Hash, PartialEq, Eq, Clone)]
pub struct ComponentField {
pub required: bool,
pub description: Option<String>,
pub field_type: FieldType,
}
#[derive(Debug, Hash, PartialEq, Eq, Clone)]
pub enum FieldType {
String,
Number,
Integer,
Boolean,
/// Name of the type in module
Type(String),
}
impl Default for FieldType {
fn default() -> Self {
Self::String
}
}
impl Components {
pub fn new() -> Self {
Self::default()
}
fn insert_request_body(&mut self, body: RequestBody) {
self.request_bodies.insert(body.name.clone(), body);
}
fn insert_schema(&mut self, component: Component) {
self.schemas.insert(component.name.clone(), component);
}
pub fn parse_schema(
&mut self,
name: &String,
schema: &ReferenceOr<Schema>,
) -> Result<(), ParseSchemaError> {
let (_, created_components) = parse_ref_or_schema(&name, &schema)?;
for component in created_components.into_iter() {
self.insert_schema(component);
}
Ok(())
}
// Parse request body from openapiv3 and add it to index
// pub fn parse_request_body(&mut self, name: &String, request_body: &openapiv3::RequestBody) {
// log::trace!("Parsing request body {}", name);
// let mut highway_body = RequestBody::default();
// highway_body.name = name.clone();
// highway_body.description = request_body.description.clone();
// highway_body.required = request_body.required;
// if let Some(media_type) = request_body.content.get("application/json") {
// if let Some(ReferenceOr::Item(schema)) = media_type.schema.clone() {
// self.parse_body_schema(&schema);
// } else {
// log::info!(
// "skipping media type {:?} in {}.content, supported only inline schema",
// media_type.schema,
// name
// );
// }
// } else {
// log::info!(
// "skipping content {:?} in {}, supported only application/json",
// request_body.content,
// name
// );
// }
// self.insert_request_body(highway_body);
// }
// fn parse_body_schema(&mut self, schema: &openapiv3::Schema) {
// log::trace!("Parsing schema {:?}", schema);
// use openapiv3::{SchemaKind, Type};
// match schema.schema_kind.clone() {
// SchemaKind::Type(internal_type) => match internal_type {
// Type::Object(object) => {
// let component = Self::parse_object_to_component(&object);
// let body = RequestBody {
// name: component.name,
// description: component.description,
// required: true,
// fields: component.fields,
// };
// self.insert_request_body(body);
// }
// unsupported_type => {
// log::info!("unsupported schema type {:?}", unsupported_type);
// }
// },
// unsupported_kind => {
// log::info!("unsupported schema kind {:?}", unsupported_kind);
// }
// }
// }
// Can't parse description and name
// fn parse_object_to_component(object: &openapiv3::ObjectType) -> Component {
// use openapiv3::{SchemaKind, Type};
// let mut component = Component::default();
// for (name, schema) in object.properties.iter() {
// let required = object.required.iter().find(|e| *e == name).is_some();
// if let ReferenceOr::Item(schema) = schema.clone().unbox() {
// match schema.schema_kind {
// SchemaKind::Type(Type::String(_string_type)) => {
// let field_type = FieldType::String;
// let field = ComponentField {
// name: name.clone(),
// description: schema.schema_data.description.clone(),
// required,
// field_type,
// };
// component.fields.insert(name.clone(), field);
// }
// unsupported_kind => {
// log::info!("skipping unsupported kind {:?}", unsupported_kind);
// continue;
// }
// }
// } else {
// log::info!("skipping property {} due to unsupported refs", name);
// }
// }
// component
// }
}
pub enum Reference {
/// References to the current file
Relative(ReferenceRelative),
/// References to the file on the current disk
File(String),
/// References to the file in the web
Remote,
}
pub enum ReferenceRelative {
Response { name: String },
Parameter { name: String },
RequestBody { name: String },
Schema { name: String },
Responses { name: String },
}
#[derive(Debug)]
pub enum ParseSchemaError {
UnsupportedType,
ReferenceNotSupported,
}
fn parse_ref_or_schema(
name: &String,
ref_or: &ReferenceOr<openapiv3::Schema>,
) -> Result<(FieldType, Vec<Component>), ParseSchemaError> {
match ref_or {
ReferenceOr::Item(schema) => parse_schema(&name, &schema),
ReferenceOr::Reference { reference } => {
log::info!(
"reference for schemas is not supported yet. Skipping {} for {}...",
reference,
name
);
Err(ParseSchemaError::ReferenceNotSupported)
}
}
}
fn parse_schema(
name: &String,
schema: &openapiv3::Schema,
) -> Result<(FieldType, Vec<Component>), ParseSchemaError> {
use inflections::Inflect;
let mut list = vec![];
use openapiv3::{SchemaKind, Type};
match &schema.schema_kind {
SchemaKind::Type(schema_type) => {
let field_type = match schema_type {
Type::Number(_number) => FieldType::Number,
Type::Integer(_integer) => FieldType::Integer,
// TODO: parse enum
Type::String(_string) => FieldType::String,
Type::Boolean {} => FieldType::Boolean,
Type::Object(object) => {
let (fields, mut created_components) = parse_schema_object(&name, object)?;
let component_name = name.clone().to_pascal_case();
let component = Component {
name: component_name.clone(),
description: schema.schema_data.description.clone(),
kind: ComponentKind::Object { fields },
};
list.push(component);
list.append(&mut created_components);
FieldType::Type(component_name.clone())
}
Type::Array(_array) => unimplemented!(),
};
Ok((field_type, list))
}
other => {
log::info!("this schema kind is not supported {:?}", other);
Err(ParseSchemaError::UnsupportedType)
}
}
}
fn parse_schema_object(
name: &String,
schema_object: &openapiv3::ObjectType,
) -> Result<(indexmap::IndexMap<String, ComponentField>, Vec<Component>), ParseSchemaError> {
use inflections::Inflect;
let mut components = vec![];
let mut fields = indexmap::IndexMap::new();
for (field_name, schema) in schema_object.properties.iter() {
let inner_name = format!("{}{}", name, field_name.clone().to_pascal_case());
let (field_type, mut created_components) =
parse_ref_or_schema(&inner_name, &schema.clone().unbox())?;
components.append(&mut created_components);
let field = ComponentField {
required: schema_object
.required
.iter()
.find(|found| *found == field_name)
.is_some(),
description: None, // TODO: parse field description
field_type,
};
fields.insert(field_name.clone(), field);
}
Ok((fields, components))
}
impl Into<printer::GeneratedModule> for Components {
fn into(self) -> printer::GeneratedModule {
let mut module = printer::GeneratedModule::default();
for (_, component) in self.schemas.into_iter() {
module.components.schemas.list.push(component.into());
}
module
}
}
use printer::components as comp;
impl Into<comp::Component> for Component {
fn into(self) -> comp::Component {
match self.kind {
ComponentKind::Object { fields } => {
let mut target_fields = vec![];
for (field_name, field) in fields.into_iter() {
target_fields.push(into_field_type(&field_name, field));
}
let component = comp::Component::Object {
name: self.name,
description: self.description,
fields: target_fields,
};
component
}
ComponentKind::String => comp::Component::Type {
name: self.name,
description: self.description,
type_value: comp::FieldType::Native(comp::NativeType::String {
format: Default::default(),
}),
},
ComponentKind::Integer => comp::Component::Type {
name: self.name,
description: self.description,
type_value: comp::FieldType::Native(comp::NativeType::Integer {
format: Default::default(),
}),
},
ComponentKind::Number => comp::Component::Type {
name: self.name,
description: self.description,
type_value: comp::FieldType::Native(comp::NativeType::Float {
format: Default::default(),
}),
},
ComponentKind::Boolean => comp::Component::Type {
name: self.name,
description: self.description,
type_value: comp::FieldType::Native(comp::NativeType::Boolean),
},
_ => unimplemented!(),
}
}
}
fn into_field_type(name: &String, field: ComponentField) -> comp::Field {
let field_type = match field.field_type {
FieldType::String => comp::FieldType::Native(comp::NativeType::String {
format: Default::default(),
}),
FieldType::Integer => comp::FieldType::Native(comp::NativeType::Integer {
format: Default::default(),
}),
FieldType::Number => comp::FieldType::Native(comp::NativeType::Float {
format: Default::default(),
}),
FieldType::Boolean => comp::FieldType::Native(comp::NativeType::Boolean),
FieldType::Type(type_path) => {
if type_path.contains("::") || type_path.contains("<") {
comp::FieldType::Internal(type_path)
} else {
comp::FieldType::Custom(type_path)
}
}
};
comp::Field {
name: name.clone(),
description: field.description,
required: field.required,
field_type,
}
}