Skip to content

Commit be946b6

Browse files
committed
fix: simplify versions.toml to consts code
1 parent 40440bb commit be946b6

File tree

1 file changed

+18
-47
lines changed

1 file changed

+18
-47
lines changed

stacks-common/build.rs

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -11,58 +11,29 @@ fn main() {
1111

1212
let mut rust_code = String::from("// Auto-generated code from versions.toml\n\n");
1313

14-
fn generate_constants(value: &Value, prefix: &str, code: &mut String) {
15-
match value {
16-
Value::Table(table) => {
17-
for (key, val) in table {
18-
let new_prefix = if prefix.is_empty() {
19-
key.to_string()
20-
} else {
21-
format!("{}_{}", prefix, key)
22-
};
23-
generate_constants(val, &new_prefix, code);
24-
}
25-
}
26-
Value::Array(arr) => {
27-
code.push_str(&format!(
28-
"pub const {}: &[&str] = &[{}];\n",
29-
prefix.to_uppercase(),
30-
arr.iter()
31-
.map(|v| format!("\"{}\"", v.as_str().unwrap_or("")))
32-
.collect::<Vec<_>>()
33-
.join(", ")
34-
));
35-
}
36-
_ => {
37-
let const_value = match value {
38-
Value::String(s) => format!("\"{}\"", s),
39-
Value::Integer(n) => n.to_string(),
40-
Value::Float(f) => f.to_string(),
41-
Value::Boolean(b) => b.to_string(),
42-
_ => "\"\"".to_string(),
14+
match config {
15+
Value::Table(table) => {
16+
for (key, val) in table {
17+
match val {
18+
Value::String(s) => {
19+
let const_value = format!("\"{}\"", s);
20+
rust_code.push_str(&format!(
21+
"pub const {}: &str = {};\n",
22+
key.to_uppercase(),
23+
const_value
24+
));
25+
}
26+
_ => {
27+
panic!("Invalid value type in versions.toml: {:?}", val);
28+
}
4329
};
44-
code.push_str(&format!(
45-
"pub const {}: {} = {};\n",
46-
prefix.to_uppercase(),
47-
if value.is_str() {
48-
"&str"
49-
} else if value.is_integer() {
50-
"i64"
51-
} else if value.is_float() {
52-
"f64"
53-
} else if value.is_bool() {
54-
"bool"
55-
} else {
56-
"&str"
57-
},
58-
const_value
59-
));
6030
}
6131
}
32+
_ => {
33+
panic!("Invalid value type in versions.toml: {:?}", config);
34+
}
6235
}
6336

64-
generate_constants(&config, "", &mut rust_code);
65-
6637
let out_dir = env::var_os("OUT_DIR").unwrap();
6738
let dest_path = Path::new(&out_dir).join("versions.rs");
6839
fs::write(&dest_path, rust_code).expect("Failed to write generated code");

0 commit comments

Comments
 (0)