use std::env;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::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.items.insert(
0,
syn::parse_quote! {
use derive_builder::Builder;
},
);
fn path_is_option(path: &syn::Path) -> bool {
let idents_of_path =
path
.segments
.iter()
.into_iter()
.fold(String::new(), |mut acc, v| {
acc.push_str(&v.ident.to_string());
acc.push('|');
acc
});
vec!["Option|", "std|option|Option|", "core|option|Option|"]
.into_iter()
.find(|s| idents_of_path == *s)
!= None
}
(&mut ast.items).iter_mut().for_each(|ref mut item| {
if let syn::Item::Struct(s) = item {
s.attrs.extend(vec![
syn::parse_quote! {
#[derive(Builder)]
},
syn::parse_quote! {
#[builder(setter(into, strip_option))]
},
]);
(&mut s.fields).into_iter().for_each(|ref mut field| {
if let syn::Type::Path(typepath) = &field.ty {
if path_is_option(&typepath.path) {
field.attrs.push(syn::parse_quote! {
#[builder(setter(into, strip_option), default)]
})
}
}
})
}
});
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("Sarif"), 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("sarif.rs"))?;
file.write_all(prettyplease::unparse(&generated).as_bytes())?;
Ok(())
}