anaxa_builder/codegen/
rust.rs1use crate::schema::{ConfigItem, ConfigType};
2use anyhow::Result;
3use std::collections::HashMap;
4use std::fmt::Write;
5
6pub fn generate_consts(
7 items: &[ConfigItem],
8 values: &HashMap<String, toml::Value>,
9) -> Result<String> {
10 let mut buffer = String::new();
11
12 writeln!(buffer, "// Generated by anaxa-config")?;
13 writeln!(buffer)?;
14 write!(buffer, "pub const ANAXA_BUILDER_VERSION: &str = ")?;
15 writeln!(buffer, "\"{}\";", env!("CARGO_PKG_VERSION"))?;
16
17 for item in items {
18 if let Some(val) = values.get(&item.name) {
19 if let Some(formatted) = item.config_type.format_value_rust(val) {
20 let rust_type = item
21 .rust_type
22 .as_deref()
23 .unwrap_or_else(|| item.config_type.rust_type());
24 writeln!(
25 buffer,
26 "#[allow(dead_code)]\n/// {}\npub const {}: {} = {};",
27 item.desc, item.name, rust_type, formatted
28 )?;
29 }
30 }
31 }
32
33 Ok(buffer)
34}
35
36pub fn generate_rust_cfgs(
38 items: &[ConfigItem],
39 values: &HashMap<String, toml::Value>,
40) -> Result<Vec<String>> {
41 let mut cfgs = Vec::new();
42
43 for item in items {
44 if let Some(val) = values.get(&item.name) {
45 if item.config_type == ConfigType::Bool && val.as_bool() == Some(true) {
46 cfgs.push(item.name.clone());
47 }
48 }
49 }
50
51 Ok(cfgs)
52}