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
use std::fs::write;
use std::path::Path;
use schemars::schema::RootSchema;
use crate::casing::to_snake_case;
pub fn export_schema(schema: &RootSchema, out_dir: &Path) {
let title = schema
.schema
.metadata
.as_ref()
.map(|b| b.title.clone().unwrap_or_else(|| "untitled".to_string()))
.unwrap_or_else(|| "unknown".to_string());
write_schema(schema, out_dir, &title);
}
pub fn export_schema_with_title(schema: &RootSchema, out_dir: &Path, title: &str) {
let mut schema = schema.clone();
if let Some(metadata) = &mut schema.schema.metadata {
metadata.title = Some(title.to_string());
}
write_schema(&schema, out_dir, title);
}
fn write_schema(schema: &RootSchema, out_dir: &Path, title: &str) {
let path = out_dir.join(format!("{}.json", to_snake_case(title)));
let json = serde_json::to_string_pretty(schema).unwrap();
write(&path, json + "\n").unwrap();
println!("Created {}", path.to_str().unwrap());
}