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
use std::path::PathBuf;
use crate::error::Error;
pub fn rust_build<P: Into<PathBuf>>(
config_path: P,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
rust_build_into(config_path, ".")
}
pub fn rust_build_into<CFG: Into<PathBuf>, OUT: Into<PathBuf>>(
config_path: CFG,
output_relative_dir: OUT,
) -> std::result::Result<(), Box<dyn std::error::Error>> {
use crate::{
config::{CodegenConfig, OutputLanguage},
Generator,
};
let config_path = config_path.into();
if !config_path.is_file() {
return Err(Error::Build(format!("missing config file {}", &config_path.display())).into());
}
let config_path = std::fs::canonicalize(config_path)?;
let config_file = std::fs::read_to_string(&config_path).map_err(|e| {
Error::Build(format!(
"error reading config file '{}': {}",
&config_path.display(),
e
))
})?;
let mut config = config_file
.parse::<CodegenConfig>()
.map_err(|e| Error::Build(format!("parsing config: {}", e)))?;
config.base_dir = config_path.parent().unwrap().to_path_buf();
config.output_languages = vec![OutputLanguage::Rust];
println!("cargo:rerun-if-changed={}", &config_path.display());
let model = crate::sources_to_model(&config.models, &config.base_dir, 0)?;
if let Err(msgs) = crate::validate::validate(&model) {
return Err(Box::new(Error::Build(msgs.join("\n"))));
}
for path in crate::sources_to_paths(&config.models, &config.base_dir, 0)?.into_iter() {
if path.exists() {
println!("cargo:rerun-if-changed={}", &path.display());
}
}
Generator::default().gen(
Some(&model),
config,
Vec::new(),
&output_relative_dir.into(),
Vec::new(),
)?;
Ok(())
}