forked from stadust/dash-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
257 lines (208 loc) · 7.83 KB
/
build.rs
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
use serde::Deserialize;
use std::{collections::HashMap, fs, fs::File, io::Write, path::Path};
fn main() {
generate_boilerplate().unwrap();
}
fn generate_boilerplate() -> std::io::Result<()> {
let out_dir = std::env::var_os("OUT_DIR").unwrap();
for entry in fs::read_dir("descriptions")? {
let entry = entry?;
if let Ok(file) = entry.file_name().into_string() {
println!("cargo:rerun-if-changed=descriptions/{}", file);
}
let file_stem = entry.path().file_stem().unwrap().to_str().unwrap().to_string();
let filename = format!("{}.boilerplate", file_stem);
let dest_path = Path::new(&out_dir).join(filename);
let mut f = File::create(dest_path)?;
let ng_yml = fs::read_to_string(entry.path())?;
let description: ModelDescription = serde_yaml::from_str(&ng_yml).unwrap();
writeln!(&mut f, "#[allow(non_upper_case_globals, unused_imports)]")?;
writeln!(&mut f, "const _{}: () = {{", file_stem)?;
write_preamble(&mut f)?;
description.write_internal_model(&mut f)?;
description.write_has_robtop_format_impl(&mut f)?;
writeln!(&mut f, "}};")?;
}
Ok(())
}
fn write_preamble<W: Write>(f: &mut W) -> std::io::Result<()> {
writeln!(f, "use crate::{{")?;
writeln!(
f,
"serde::{{DeError, HasRobtopFormat, IndexedDeserializer, IndexedSerializer, PercentDecoded, SerError, Thunk, RefThunk, \
Base64Decoded}},"
)?;
writeln!(f, "}};")?;
writeln!(f, "use serde::{{Deserialize, Serialize}};")?;
writeln!(f, "use std::{{borrow::{{Cow, Borrow}}, io::Write}};")?;
Ok(())
}
#[derive(Deserialize, Debug)]
struct ModelDescription {
r#struct: String,
map_like: bool,
separator: String,
indices: Vec<Index>,
#[serde(default)]
special_fields: HashMap<String, String>,
}
impl ModelDescription {
fn struct_name(&self) -> &str {
match self.r#struct.split('<').next() {
Some(first_part) => first_part,
None => &self.r#struct,
}
}
fn has_any_thunks(&self) -> bool {
self.indices.iter().any(|idx| idx.thunk)
}
pub fn write_internal_model<W: Write>(&self, f: &mut W) -> std::io::Result<()> {
writeln!(f, "#[derive(Serialize, Deserialize)]")?;
if self.has_any_thunks() {
writeln!(f, "struct Internal{}<'src, 'bor> {{", self.struct_name())?;
} else {
writeln!(f, "struct Internal{}<'src> {{", self.struct_name())?;
}
for index in &self.indices {
index.write_as_field(f)?;
}
writeln!(f, "}}")?;
Ok(())
}
pub fn write_has_robtop_format_impl<W: Write>(&self, f: &mut W) -> std::io::Result<()> {
writeln!(f, "impl<'src> HasRobtopFormat<'src> for {} {{", self.r#struct)?;
writeln!(f, "fn from_robtop_str(input: &'src str) -> Result<Self, DeError> {{")?;
writeln!(
f,
"let internal = Internal{}::deserialize(&mut IndexedDeserializer::new(input, \"{}\", {}))?;",
self.struct_name(),
self.separator,
self.map_like
)?;
writeln!(f, "Ok(Self {{")?;
for index in &self.indices {
if let Some(ref corresponding_field) = index.maps_to {
index.generate_from_robtop_conversion(f, corresponding_field)?;
}
}
for (field, code) in &self.special_fields {
writeln!(f, "{}: {},", field, code)?;
}
writeln!(f, "}})")?;
writeln!(f, "}}")?; // end method
writeln!(f, "fn write_robtop_data<W: Write>(&self, writer: W) -> Result<(), SerError> {{")?;
writeln!(f, "let internal = Internal{} {{", self.struct_name())?;
for index in &self.indices {
if index.maps_to.is_none() == index.compute.is_none() {
panic!(
"An index can either map 1-to-1 to a dash-rs field, or it can be computed dynamically, not both or neither. Index: {}",
index.value
);
}
if let Some(ref mapsto) = index.maps_to {
index.generate_to_robtop_conversion(f, mapsto)?;
} else if let Some(ref code) = index.compute {
writeln!(f, "index_{}: {},", index.value, code)?;
}
}
writeln!(f, "}};")?;
writeln!(
f,
"internal.serialize(&mut IndexedSerializer::new(\"{}\", writer, {}))",
self.separator, self.map_like
)?;
writeln!(f, "}}")?; // end method
writeln!(f, "}}")?; // end impls
Ok(())
}
}
#[derive(Deserialize, Debug)]
struct Index {
value: u8,
r#type: String,
#[serde(default)]
thunk: bool,
#[serde(default)]
optional: bool, // only relevant for thunks!
#[serde(default)]
maps_to: Option<String>,
#[serde(default)]
use_into: bool,
#[serde(default)]
compute: Option<String>,
#[serde(default)]
attributes: Vec<String>,
}
impl Index {
pub fn write_as_field<W: Write>(&self, f: &mut W) -> std::io::Result<()> {
for attr in &self.attributes {
writeln!(f, "#[serde({})]", attr)?;
}
writeln!(f, "#[serde(rename = \"{}\")]", self.value)?;
if self.thunk {
if self.optional {
writeln!(f, "index_{}: Option<RefThunk<'src, 'bor, {}>>,", self.value, self.r#type)?;
} else {
writeln!(f, "index_{}: RefThunk<'src, 'bor, {}>,", self.value, self.r#type)?;
}
} else {
writeln!(f, "index_{}: {},", self.value, self.r#type)?;
}
Ok(())
}
pub fn generate_from_robtop_conversion<W: Write>(&self, f: &mut W, field_name: &str) -> std::io::Result<()> {
write!(f, "{}: ", field_name)?;
if self.thunk {
if self.optional {
write!(
f,
"match internal.index_{} {{None => None, Some(RefThunk::Unprocessed(unproc)) => Some(Thunk::Unprocessed(unproc)), _ \
=> unreachable!()}}",
self.value
)?;
} else {
write!(
f,
"Thunk::Unprocessed(match internal.index_{} {{RefThunk::Unprocessed(unproc) => unproc, _ => unreachable!() }})",
self.value
)?;
}
} else {
match &self.r#type[..] {
"&'src str" => write!(f, "Cow::Borrowed(internal.index_{})", self.value)?,
"Option<&'src str>" => write!(f, "internal.index_{}.map(Cow::Borrowed)", self.value)?,
_ =>
if self.use_into {
write!(f, "internal.index_{}.into()", self.value)?
} else {
write!(f, "internal.index_{}", self.value)?
},
}
}
writeln!(f, ",")?;
Ok(())
}
pub fn generate_to_robtop_conversion<W: Write>(&self, f: &mut W, field_name: &str) -> std::io::Result<()> {
write!(f, "index_{}: ", self.value)?;
if self.thunk {
if self.optional {
write!(f, "self.{}.as_ref().map(|t| t.as_ref_thunk())", field_name)?;
} else {
write!(f, "self.{}.as_ref_thunk()", field_name)?;
}
} else {
match &self.r#type[..] {
"&'src str" => write!(f, "self.{}.as_ref()", field_name)?,
"Option<&'src str>" => write!(f, "self.{}.as_deref()", field_name)?,
_ =>
if self.use_into {
write!(f, "self.{}.into()", field_name)?
} else {
write!(f, "self.{}", field_name)?
},
}
}
writeln!(f, ",")?;
Ok(())
}
}