ruddle/pocs/json_schema/src/main.rs
2022-08-20 17:39:01 +02:00

46 lines
1.5 KiB
Rust

use std::collections::HashMap;
fn main() -> anyhow::Result<()> {
println!("Hello, json_schemas!");
let mut base_schema: serde_json::Value = load_json("base.schema.json")?;
let actions_schema: serde_json::Value = load_json("actions.schema.json")?;
let mut defs: HashMap<String, serde_json::Value> = HashMap::new();
defs.insert("actions".to_string(), actions_schema);
base_schema["$defs"] = serde_json::to_value(defs)?;
let mut plugins: HashMap<String, serde_json::Value> = HashMap::new();
plugins.insert("ruddle/actions@1.0.0".into(), refs("#/$defs/actions")?);
base_schema["properties"]["plugins"]["type"] = serde_json::to_value("object")?;
base_schema["properties"]["plugins"]["properties"] = serde_json::to_value(plugins)?;
base_schema["properties"]["plugins"]["additionalProperties"] = serde_json::to_value(false)?;
let schema = std::fs::File::create("schema.json")?;
let file_writer = std::io::BufWriter::new(schema);
serde_json::to_writer(file_writer, &base_schema)?;
Ok(())
}
fn refs(content: &str) -> anyhow::Result<serde_json::Value> {
let mut refs = serde_json::Value::default();
refs["$ref"] = serde_json::to_value(content)?;
Ok(refs)
}
fn load_json(file_name: &str) -> anyhow::Result<serde_json::Value> {
let file = std::fs::File::open(file_name)?;
let file_reader = std::io::BufReader::new(file);
let schema: serde_json::Value = serde_json::from_reader(file_reader)?;
println!("printing schema: {}", file_name);
println!("{:#?}", schema);
Ok(schema)
}