weld_codegen/
rust_build.rs1use std::path::PathBuf;
7
8use crate::error::Error;
9
10pub fn rust_build<P: Into<PathBuf>>(
12 config_path: P,
13) -> std::result::Result<(), Box<dyn std::error::Error>> {
14 rust_build_into(config_path, ".")
15}
16
17pub fn rust_build_into<CFG: Into<PathBuf>, OUT: Into<PathBuf>>(
23 config_path: CFG,
24 output_relative_dir: OUT,
25) -> std::result::Result<(), Box<dyn std::error::Error>> {
26 use crate::{
27 config::{CodegenConfig, OutputLanguage},
28 Generator,
29 };
30 let config_path = config_path.into();
31 if !config_path.is_file() {
32 return Err(Error::Build(format!("missing config file {}", &config_path.display())).into());
33 }
34 let config_path = std::fs::canonicalize(config_path)?;
35 let config_file = std::fs::read_to_string(&config_path).map_err(|e| {
36 Error::Build(format!(
37 "error reading config file '{}': {}",
38 &config_path.display(),
39 e
40 ))
41 })?;
42 let mut config = config_file
43 .parse::<CodegenConfig>()
44 .map_err(|e| Error::Build(format!("parsing config: {e}")))?;
45 config.base_dir = config_path.parent().unwrap().to_path_buf();
46 config.output_languages = vec![OutputLanguage::Rust];
47
48 println!("cargo:rerun-if-changed={}", &config_path.display());
50
51 let model = crate::sources_to_model(&config.models, &config.base_dir, 0)?;
52
53 if let Err(msgs) = crate::validate::validate(&model) {
54 return Err(Box::new(Error::Build(msgs.join("\n"))));
55 }
56
57 for path in crate::sources_to_paths(&config.models, &config.base_dir, 0)?.into_iter() {
60 if path.exists() {
68 println!("cargo:rerun-if-changed={}", &path.display());
69 }
70 }
71
72 Generator::default().gen(
73 Some(&model),
74 config,
75 Vec::new(),
76 &output_relative_dir.into(),
77 Vec::new(),
78 )?;
79
80 Ok(())
81}