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
use crate::error::Error;
use std::path::PathBuf;
pub fn rust_build<P: Into<PathBuf>>(
config_path: P,
) -> 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.to_string())))?;
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 out_dir = PathBuf::from(".");
let model = crate::sources_to_model(&config.models, &config.base_dir, 0)?;
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(), &out_dir, Vec::new())?;
Ok(())
}