This repository has been archived by the owner on Jun 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
410 lines (368 loc) · 13.9 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#![feature(iter_intersperse)]
use itertools::Itertools;
use std::{
collections::BTreeMap,
fs::File,
path::Path,
io::Write
};
use common_serde::*;
fn main() {
println!("cargo:rerun-if-changed=src/si/quantities_definition/");
println!("cargo:rerun-if-changed=src/si/constants_definition/");
println!("cargo:rerun-if-changed=src/si/prefixes/");
println!("cargo:rerun-if-changed=types.toml");
let path_si = Path::new("src/si");
let path_prefixes = path_si.join("prefixes");
let path_quantities_code = path_si.join("quantities_code");
let cargo_toml = Path::new("types.toml");
let storage_types = common_serde::load_storage_types(cargo_toml);
let path_quantities_definition = Path::new("src/si/quantities_definition");
let quantities = common_serde::load_quantities_from_dir(path_quantities_definition);
let path_quantity_si_extended_code = path_quantities_code.join("quantities_si_extended.rs");
let path_quantity_si_isq_code = path_quantities_code.join("quantities_si_isq.rs");
let path_prefixes_custom_code = path_prefixes.join("custom.rs");
let path_prefixes_si_data = path_prefixes.join("metric.toml");
let path_prefixes_si_code = path_prefixes.join("metric.rs");
let prefixes_si = common_serde::load_prefixes(&path_prefixes_si_data);
generate_prefixes(&prefixes_si, &path_prefixes_si_code);
let path_prefixes_bin_data = path_prefixes.join("bin.toml");
let path_prefixes_bin_code = path_prefixes.join("bin.rs");
let prefixes_bin = common_serde::load_prefixes(&path_prefixes_bin_data);
generate_prefixes(&prefixes_bin, &path_prefixes_bin_code);
generate_quantities(
&quantities,
&path_quantity_si_extended_code,
common_serde::SystemOfQuantities::SiExtended,
);
// generate_quantities(
// &quantities,
// &path_quantity_si_isq_code,
// SystemOfQuantities::SiIsq,
// );
// let units_si_extended_path = path_si.join("aliases_to_units_si_extended.bin");
// let units_si_isq_path = path_si.join("aliases_to_units_si_isq.bin");
generate_aliases_to_units(
quantities.clone().as_mut(),
&path_prefixes_custom_code,
SystemOfQuantities::SiExtended,
&prefixes_si,
&prefixes_bin,
);
// generate_units(
// &quantities,
// &units_si_isq_path,
// SystemOfQuantities::SiIsq,
// );
// let codata_src_path = Path::new("src/si/constants_definition/codata2018.txt");
// let out_dir_path = Path::new(&out_dir);
// //generate_codata_constants(codata_src_path, out_dir_path);
// //generate_codata_constants(codata_src_path, out_dir_path);
}
fn generate_quantities(
qs: &[common_serde::Quantity],
path: &Path,
system_of_quantities: common_serde::SystemOfQuantities,
) {
let mut output = File::create(path).unwrap();
writeln!(
output,
"
use crate::{{prefixes::*, DisplayUnitBase, UnitSpecialization}};
use crate::{{BaseUnit, Quantity, SiUnitExt, SiUnit}};
use units_macro::{{generate_units_name}};
"
)
.unwrap();
for q in qs.iter() {
let common_serde::Quantity {
names,
symbol,
short_dim_formula,
long_dim_formula,
units_formula,
..
} = q;
let common_serde::Dimensions {
L,
M,
T,
I,
Θ,
N,
J,
A,
ΔΘ,
INFO,
} = q.dimensions;
let name = &names["en"];
writeln!(
output,
"/// {name}, {short_dim_formula}, \\[{units_formula}\\];\n/// {long_dim_formula}"
)
.unwrap();
match system_of_quantities {
common_serde::SystemOfQuantities::SiIsq => {
let temp = Θ + ΔΘ;
writeln!(
output,
"pub const {symbol} : SiUnit = SiUnit {{{L}, {M}, {T}, {I}, {temp} , {N}, {J} , {name}}};").unwrap()
}
common_serde::SystemOfQuantities::SiExtended => {
writeln!(
output,
"pub const {symbol} : SiUnit = SiUnitExt{{ {L}, {M}, {T}, {I}, {Θ} , {N}, {J}, {A}, {ΔΘ}, {INFO}, {name}}};")
.unwrap();
}
}
}
}
// fn generate_codata_constants<Storage: Float + FromStr + Display>(codata: &Path, out_dir: &Path) {
// let codata = File::open(codata).unwrap();
// let codata = BufReader::new(codata);
// let storage_type = std::any::type_name::<Storage>();
// let of = out_dir.join(format!("codata2018_{storage_type}.rs"));
// let of = Path::new(&of);
// let mut of = File::create(of).unwrap();
// for line in codata.lines() {
// let line = line.unwrap();
// let name: String = line[..60]
// .trim()
// .replace(' ', "_")
// .replace('.', "")
// .replace('-', "_")
// .replace([',', '(', ')'], "")
// .replace('/', "_PER_")
// .to_uppercase();
// let name_pretty = line[..60]
// .trim()
// .replace(" mag.", " magnetic")
// .replace(" mom.", " moment");
// let value = line[60..85].trim().replace(' ', "_").replace("...", "");
// let unit = line[110..].trim();
// let quantity = quantity_from_dimension(unit);
// if let Some(quantity) = quantity {
// let value = if value.contains('.') {
// value
// } else {
// format!("{value}.0")
// };
// let value_numeric = Storage::from_str(&value.trim().replace('_', ""));
// if let Ok(value_numeric) = value_numeric {
// if !(value_numeric.is_finite() && value_numeric.is_normal()) {
// continue;
// }
// } else {
// continue;
// }
// let unit_pretty = if unit.is_empty() {
// "dimensionless".to_string()
// } else {
// unit.replace(' ', "⋅")
// .replace("^-1", "<sup>-1</sup>")
// .replace("^-2", "<sup>-2</sup>")
// .replace("^-3", "<sup>-3</sup>")
// .replace("^-4", "<sup>-4</sup>")
// .replace("^2", "<sup>2</sup>")
// .replace("^3", "<sup>3</sup>")
// .replace("^4", "<sup>4</sup>")
// };
// let value_pretty = if let Some((mantissa, order)) = value.split_once("_e") {
// format!("{mantissa}⋅10<sup>{order}</sup>")
// } else {
// value.clone()
// };
// writeln!(
// of,
// "/// {name_pretty}, {value_pretty} \\[{unit_pretty}\\]"
// )
// .unwrap();
// writeln!(
// of,
// "pub const {name}: {quantity} = {quantity}::new({value});"
// )
// .unwrap();
// } else {
// // println!("cargo:warning=Unknown unit {unit}");
// };
// }
// }
fn generate_aliases_to_units(
qs: &mut [common_serde::Quantity],
path_custom_prefixes: &Path,
system_of_quantities: common_serde::SystemOfQuantities,
prefixes_metric: &[common_serde::Prefix],
prefixes_binary: &[common_serde::Prefix],
) {
let mut output_custom_prefixes = File::create(path_custom_prefixes).unwrap();
let map = &mut UnitsMap::new();
for q in qs.iter() {
let common_serde::Quantity {
derive_metric_prefixes,
derive_binary_prefixes,
units,
..
} = q;
let mut units = units.clone();
if let Some(unit_names) = derive_metric_prefixes {
let units_derived_metric = derive_prefixes(prefixes_metric, unit_names, &units);
units.append(&mut units_derived_metric.clone());
}
if let Some(unit_names) = derive_binary_prefixes {
let units_derived_binary = derive_prefixes(prefixes_binary, unit_names, &units);
units.append(&mut units_derived_binary.clone());
}
//now all units for this quantity are stored in the BTreeMap
for (base_name, u) in q.units.iter() {
let u_clone = u.clone();
let common_serde::Unit {
numerator,
denominator,
//symbol: symbol_base,
names,
..
} = u;
//dont create a new prefix, if an prefix already exists
let mut prefix_name = None;
prefixes_metric.iter().chain(prefixes_binary).for_each(
|Prefix {
aliases,
numerator,
denominator,
}| {
if numerator.clone() == u_clone.numerator && denominator.clone() == u_clone.denominator {
prefix_name = Some(aliases.clone().unwrap()[0].clone());
}
},
);
//sets the name of the Prefix
let prefix_name =
match prefix_name{
Some(prefix_name) => prefix_name,
None => {
let prefix_name = format!("{base_name}_prefix");
writeln!(
output_custom_prefixes,
"prefix!({prefix_name},{numerator},{denominator})"
)
.unwrap();
prefix_name
},
};
//generates the mappings: mm -> milli, meter
for UnitName { singular, .. } in names.clone().unwrap().values(){
map.insert(singular.clone(), (prefix_name.clone(), base_name.clone()));
}
}
}
common_serde::save_units_map(map.clone());
}
fn derive_prefixes_unit(prefixes: &[common_serde::Prefix], unit: Unit) -> BTreeMap<String, Unit> {
let to_simplify = prefixes.iter().map(|prefix| {
(
prefix.aliases.clone().unwrap(),
Unit {
numerator: (string_exponentiate(unit.numerator.clone())
* string_exponentiate(prefix.numerator.clone()))
.to_string(),
denominator: (string_exponentiate(unit.denominator.clone())
* string_exponentiate(prefix.denominator.clone()))
.to_string(),
names: Some({
let mut prefix_aliases = prefix.aliases.clone().unwrap();
let mut map = BTreeMap::new();
let iterator = prefix_aliases
.iter()
.map(|name_prefix| {
unit.names
.clone()
.unwrap()
.iter()
.map(|(lang, UnitName { singular, plural })| {
(
lang.clone(),
UnitName {
singular: format!("{name_prefix}{singular}"),
plural: format!("{name_prefix}{plural}"),
},
)
})
.collect_vec()
})
.flatten();
for (key, value) in iterator.into_iter() {
map.insert(key, value);
}
map
}),
symbol: {
match &unit.symbol {
Some(symbol) => {
Some(format!("{}{}", prefix.aliases.clone().unwrap()[0], symbol))
}
None => None,
}
},
symbol_plural: {
match &unit.symbol_plural {
Some(symbol) => {
Some(format!("{}{}", prefix.aliases.clone().unwrap()[0], symbol))
}
None => None,
}
},
},
)
});
let map_iter = to_simplify
.map(|(prefix_names, unit)| {
prefix_names
.iter()
.map(|current_prefix_name| (current_prefix_name.clone(), unit.clone()))
.collect_vec()
})
.flatten();
let mut map = BTreeMap::new();
for (key, value) in map_iter {
map.insert(key.clone(), value);
}
map
}
fn derive_prefixes(
prefixes: &[common_serde::Prefix],
units_to_suffix: &Vec<String>,
units: &BTreeMap<String, Unit>,
) -> BTreeMap<String, Unit> {
let mut map = BTreeMap::new();
let units_map = units_to_suffix
.iter()
.map(|current_unit_id| units[current_unit_id].clone())
.map(|current_unit| derive_prefixes_unit(prefixes, current_unit))
.flatten();
for (key, value) in units_map {
map.insert(key, value);
}
map
}
fn generate_prefixes(prefixes: &[common_serde::Prefix], path: &Path) {
let mut output = File::create(path).unwrap();
writeln!(
output,
"use units_macro::prefix;
use crate::{{BaseUnit, Rational128}};
"
)
.unwrap();
for common_serde::Prefix {
aliases: names,
numerator,
denominator,
} in prefixes.into_iter()
{
let names_s: String =
Itertools::intersperse(names.clone().unwrap().iter(), &"|".to_string())
.map(|s| s.clone())
.collect();
writeln!(output, "prefix!({names_s},{numerator},{denominator});").unwrap();
}
}