use typify::{TypeSpace, TypeSpaceSettings};
use std::fs;
use std::path::Path;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("cargo:rerun-if-changed=build.rs");
let schema_targets = [
(
"assets/SelectionList_2_0_0.schema.json",
"src/generated/ssvc/selection_list.rs",
),
(
"assets/DecisionPoint_2_0_0.schema.json",
"src/generated/ssvc/decision_point.rs",
),
];
for (file_path, target_folder) in schema_targets {
println!("cargo:rerun-if-changed={file_path}");
build_from_schema(file_path, target_folder)?;
}
Ok(())
}
fn build_from_schema(
file_path: &str,
target_path: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let file = std::fs::File::open(file_path)?;
let schema = serde_json::from_reader(file)?;
let mut type_space = TypeSpace::new(
TypeSpaceSettings::default()
.with_struct_builder(true)
.with_derive("PartialEq".into())
.with_derive("Eq".into()),
);
type_space.add_root_schema(schema)?;
let syn_file = syn::parse2::<syn::File>(type_space.to_stream())?;
let content = prettyplease::unparse(&syn_file);
let out_file = Path::new(&target_path).to_path_buf();
fs::create_dir_all(&out_file.parent().unwrap())?;
fs::write(out_file, content)?;
Ok(())
}