cosmwasm_schema/
export.rs1use std::fs::write;
4use std::path::Path;
5
6use schemars::schema::RootSchema;
7
8use crate::casing::to_snake_case;
9
10pub fn export_schema(schema: &RootSchema, out_dir: &Path) {
12 let title = schema
13 .schema
14 .metadata
15 .as_ref()
16 .map(|b| b.title.clone().unwrap_or_else(|| "untitled".to_string()))
17 .unwrap_or_else(|| "unknown".to_string());
18 write_schema(schema, out_dir, &title);
19}
20
21pub fn export_schema_with_title(schema: &RootSchema, out_dir: &Path, title: &str) {
24 let mut schema = schema.clone();
25 if let Some(metadata) = &mut schema.schema.metadata {
27 metadata.title = Some(title.to_string());
28 }
29 write_schema(&schema, out_dir, title);
30}
31
32fn write_schema(schema: &RootSchema, out_dir: &Path, title: &str) {
35 let path = out_dir.join(format!("{}.json", to_snake_case(title)));
37 let json = serde_json::to_string_pretty(schema).unwrap();
38 write(&path, json + "\n").unwrap();
39 println!("Created {}", path.to_str().unwrap());
40}