-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbuild.rs
More file actions
103 lines (89 loc) · 2.95 KB
/
Copy pathbuild.rs
File metadata and controls
103 lines (89 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
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;
// Add additional items to the generated spdx.rs file
// Currently adds: derive(Builder) to each struct
// and appropriate use statements at the top of the file
// todo: this (and other parts) need a refactor and tests
fn process_token_stream(input: proc_macro2::TokenStream) -> syn::File {
let mut ast: syn::File = syn::parse2(input).unwrap();
// add use directives to top of the file
ast.items.insert(
0,
syn::parse_quote! {
use serde::{Serialize, Deserialize};
},
);
ast.items.insert(
0,
syn::parse_quote! {
use derive_builder::Builder;
},
);
// Checks if the type is an Option type (returns true if yes, false otherwise)
fn path_is_option(path: &syn::Path) -> bool {
let idents_of_path =
path.segments.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()
.any(|s| idents_of_path == *s)
}
ast.items.iter_mut().for_each(|ref mut item| {
if let syn::Item::Struct(s) = item {
// add builder attributes to each struct
s.attrs.extend(vec![
syn::parse_quote! {
#[derive(Builder)]
},
syn::parse_quote! {
#[builder(setter(into, strip_option))]
},
]);
// for each struct field, if that field is Optional, set None
// as the default value when using the builder
(&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)]
});
// field.attrs.push(syn::parse_quote! {
// #[serde(skip_serializing_if = "Option::is_none")]
// })
}
}
});
}
});
ast
}
fn generate_schema(version_str: &str) -> Result<()> {
println!("cargo:rerun-if-changed=schemas/spdx_{version_str}.json",);
let path_str = format!("schemas/spdx_{version_str}.json",);
let path = Path::new(&path_str);
// Generate the Rust schema struct
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("Spdx"), path_str, &schema);
let generated = process_token_stream(expander.expand(&schema));
// Write the struct to the $OUT_DIR/spdx.rs file.
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
let mut file =
File::create(out_path.join(format!("spdx_{version_str}.rs",)))?;
file.write_all(prettyplease::unparse(&generated).as_bytes())?;
Ok(())
}
fn main() -> Result<()> {
generate_schema("2_3")?;
Ok(())
}