anaxa_builder/codegen/
rust.rs

1use 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
15    for item in items {
16        if let Some(val) = values.get(&item.name) {
17            if let Some(formatted) = item.config_type.format_value_rust(val) {
18                writeln!(
19                    buffer,
20                    "#[allow(dead_code)]\npub const {}: {} = {};",
21                    item.name,
22                    item.config_type.rust_type(),
23                    formatted
24                )?;
25            }
26        }
27    }
28
29    Ok(buffer)
30}
31
32/// Generates a vector of strings suitable for `--cfg` flags.
33pub fn generate_rust_cfgs(
34    items: &[ConfigItem],
35    values: &HashMap<String, toml::Value>,
36) -> Result<Vec<String>> {
37    let mut cfgs = Vec::new();
38
39    for item in items {
40        if let Some(val) = values.get(&item.name) {
41            if item.config_type == ConfigType::Bool && val.as_bool() == Some(true) {
42                cfgs.push(item.name.clone());
43            }
44        }
45    }
46
47    Ok(cfgs)
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53    use crate::schema::ConfigType;
54
55    #[test]
56    fn test_generate_rust_consts() -> Result<()> {
57        let items = vec![
58            ConfigItem {
59                name: "ENABLE_A".to_string(),
60                config_type: ConfigType::Bool,
61                default: None,
62                desc: "A".to_string(),
63                depends_on: None,
64                help: None,
65                options: None,
66                feature: None,
67                range: None,
68                regex: None,
69            },
70            ConfigItem {
71                name: "STR_VAL".to_string(),
72                config_type: ConfigType::String,
73                default: None,
74                desc: "S".to_string(),
75                depends_on: None,
76                help: None,
77                options: None,
78                feature: None,
79                range: None,
80                regex: None,
81            },
82        ];
83
84        let mut values = HashMap::new();
85        values.insert("ENABLE_A".to_string(), toml::Value::Boolean(true));
86        values.insert(
87            "STR_VAL".to_string(),
88            toml::Value::String("hello".to_string()),
89        );
90
91        let code = generate_consts(&items, &values)?;
92        assert!(code.contains("pub const ENABLE_A: bool = true;"));
93        assert!(code.contains("pub const STR_VAL: &str = \"hello\";"));
94        Ok(())
95    }
96
97    #[test]
98    fn test_generate_rust_cfgs() -> Result<()> {
99        let items = vec![ConfigItem {
100            name: "ENABLE_A".to_string(),
101            config_type: ConfigType::Bool,
102            default: None,
103            desc: "A".to_string(),
104            depends_on: None,
105            help: None,
106            options: None,
107            feature: None,
108            range: None,
109            regex: None,
110        }];
111
112        let mut values = HashMap::new();
113        values.insert("ENABLE_A".to_string(), toml::Value::Boolean(true));
114
115        let cfgs = generate_rust_cfgs(&items, &values)?;
116        assert_eq!(cfgs, vec!["ENABLE_A".to_string()]);
117        Ok(())
118    }
119}