|
| 1 | +use heck::ToUpperCamelCase; |
| 2 | +use indexmap::IndexMap; |
| 3 | +use std::path::Path; |
| 4 | +use std::{env, fs}; |
| 5 | + |
1 | 6 | fn main() { |
2 | 7 | cfg_aliases::cfg_aliases! { |
3 | 8 | vfox: { any(feature = "vfox", target_os = "windows") }, |
4 | 9 | asdf: { any(feature = "asdf", not(target_os = "windows")) }, |
5 | 10 | } |
6 | 11 | built::write_built_file().expect("Failed to acquire build-time information"); |
| 12 | + |
| 13 | + codegen_settings(); |
| 14 | +} |
| 15 | + |
| 16 | +fn codegen_settings() { |
| 17 | + let out_dir = env::var_os("OUT_DIR").unwrap(); |
| 18 | + let dest_path = Path::new(&out_dir).join("settings.rs"); |
| 19 | + let mut lines = vec![r#" |
| 20 | +#[derive(Config, Default, Debug, Clone, Serialize)] |
| 21 | +#[config(partial_attr(derive(Clone, Serialize, Default)))] |
| 22 | +pub struct Settings {"# |
| 23 | + .to_string()]; |
| 24 | + |
| 25 | + let settings: toml::Table = fs::read_to_string("settings.toml") |
| 26 | + .unwrap() |
| 27 | + .parse() |
| 28 | + .unwrap(); |
| 29 | + let props_to_code = |key: &str, props: &toml::Value| { |
| 30 | + let mut lines = vec![]; |
| 31 | + let props = props.as_table().unwrap(); |
| 32 | + if let Some(description) = props.get("description") { |
| 33 | + lines.push(format!(" /// {}", description.as_str().unwrap())); |
| 34 | + } |
| 35 | + if let Some(type_) = props.get("type") { |
| 36 | + let mut opts = IndexMap::new(); |
| 37 | + if let Some(env) = props.get("env") { |
| 38 | + opts.insert("env".to_string(), env.to_string()); |
| 39 | + } |
| 40 | + if let Some(default) = props.get("default") { |
| 41 | + opts.insert("default".to_string(), default.to_string()); |
| 42 | + } else if type_.as_str().unwrap() == "bool" { |
| 43 | + opts.insert("default".to_string(), "false".to_string()); |
| 44 | + } |
| 45 | + if let Some(parse_env) = props.get("parse_env") { |
| 46 | + opts.insert( |
| 47 | + "parse_env".to_string(), |
| 48 | + parse_env.as_str().unwrap().to_string(), |
| 49 | + ); |
| 50 | + } |
| 51 | + dbg!(&opts); |
| 52 | + lines.push(format!( |
| 53 | + " #[config({})]", |
| 54 | + opts.iter() |
| 55 | + .map(|(k, v)| format!("{k} = {v}")) |
| 56 | + .collect::<Vec<_>>() |
| 57 | + .join(", ") |
| 58 | + )); |
| 59 | + lines.push(format!(" pub {}: {},", key, type_.as_str().unwrap())); |
| 60 | + } else { |
| 61 | + lines.push(" #[config(nested)]".to_string()); |
| 62 | + lines.push(format!( |
| 63 | + " pub {}: Settings{},", |
| 64 | + key, |
| 65 | + key.to_upper_camel_case() |
| 66 | + )); |
| 67 | + } |
| 68 | + lines.join("\n") |
| 69 | + }; |
| 70 | + for (key, props) in &settings { |
| 71 | + lines.push(props_to_code(key, props)); |
| 72 | + } |
| 73 | + lines.push("}".to_string()); |
| 74 | + |
| 75 | + let nested_settings = settings |
| 76 | + .iter() |
| 77 | + .filter(|(_, v)| !v.as_table().unwrap().contains_key("type")) |
| 78 | + .collect::<Vec<_>>(); |
| 79 | + for (child, props) in nested_settings { |
| 80 | + lines.push(format!( |
| 81 | + r#"#[derive(Config, Default, Debug, Clone, Serialize)] |
| 82 | +#[config(partial_attr(derive(Clone, Serialize, Default)))] |
| 83 | +#[config(partial_attr(serde(deny_unknown_fields)))] |
| 84 | +pub struct Settings{name} {{ |
| 85 | +"#, |
| 86 | + name = child.to_upper_camel_case() |
| 87 | + )); |
| 88 | + |
| 89 | + for (key, props) in props.as_table().unwrap() { |
| 90 | + lines.push(props_to_code(key, props)); |
| 91 | + } |
| 92 | + lines.push("}".to_string()); |
| 93 | + } |
| 94 | + |
| 95 | + fs::write(&dest_path, lines.join("\n")).unwrap(); |
7 | 96 | } |
0 commit comments