use std::env; use std::fs::File; use std::io::Write; use std::path::{Path, PathBuf}; use anyhow::Result; use schemafy_lib::Expander; use schemafy_lib::Schema; fn process_token_stream(input: proc_macro2::TokenStream) -> syn::File { let mut ast: syn::File = syn::parse2(input).unwrap(); ast.items.insert( 0, syn::parse_quote! { use serde::{Serialize, Deserialize}; }, ); ast } fn main() -> Result<()> { println!("cargo:rerun-if-changed=src/schema.json"); let path = Path::new("src/schema.json"); let json = std::fs::read_to_string(path).unwrap(); let schema: Schema = serde_json::from_str(&json)?; let path_str = path.to_str().unwrap(); let mut expander = Expander::new(Some("lwjson"), path_str, &schema); let generated = process_token_stream(expander.expand(&schema)); let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); let mut file = File::create(out_path.join("lwjson.rs"))?; file.write_all(prettyplease::unparse(&generated).as_bytes())?; Ok(()) }