|
| 1 | +use syn::LitStr; |
| 2 | + |
| 3 | +use super::parser::{FieldDecl, SectionDecl}; |
| 4 | +use super::value::{expression_value, schema_type}; |
| 5 | +use crate::{canonical_key, SchemaError}; |
| 6 | + |
| 7 | +pub(super) fn build(sections: &[SectionDecl]) -> Result<String, SchemaError> { |
| 8 | + let mut document = toml::Table::new(); |
| 9 | + document.insert("mod".to_owned(), toml::Value::Table(mod_metadata())); |
| 10 | + document.insert("ui".to_owned(), toml::Value::Table(ui_metadata(sections))); |
| 11 | + let visible = sections |
| 12 | + .iter() |
| 13 | + .filter(|section| should_export(section)) |
| 14 | + .map(section_value) |
| 15 | + .collect::<Result<Vec<_>, _>>()?; |
| 16 | + let mut config = toml::Table::new(); |
| 17 | + config.insert("sections".to_owned(), toml::Value::Array(visible)); |
| 18 | + document.insert("config".to_owned(), toml::Value::Table(config)); |
| 19 | + toml::to_string_pretty(&document).map_err(SchemaError::Serialize) |
| 20 | +} |
| 21 | + |
| 22 | +fn mod_metadata() -> toml::Table { |
| 23 | + let mut table = toml::Table::new(); |
| 24 | + for (key, value) in [ |
| 25 | + ("id", "applesaber.applechu"), |
| 26 | + ("name", "AppleChu"), |
| 27 | + ("version", env!("CARGO_PKG_VERSION")), |
| 28 | + ("homepage", "https://github.com/MuNET-OSS/AppleChu"), |
| 29 | + ("license", "Apache-2.0"), |
| 30 | + ("min_loader_version", "1.0.0"), |
| 31 | + ] { |
| 32 | + table.insert(key.to_owned(), toml::Value::String(value.to_owned())); |
| 33 | + } |
| 34 | + table.insert( |
| 35 | + "authors".to_owned(), |
| 36 | + toml::Value::Array(vec![toml::Value::String("Applesaber".to_owned())]), |
| 37 | + ); |
| 38 | + table.insert( |
| 39 | + "game_versions".to_owned(), |
| 40 | + toml::Value::Array(vec![toml::Value::String("2.45".to_owned())]), |
| 41 | + ); |
| 42 | + table.insert( |
| 43 | + "description".to_owned(), |
| 44 | + localized("CHUNITHM Mod", Some("CHUNITHM Mod")), |
| 45 | + ); |
| 46 | + table |
| 47 | +} |
| 48 | + |
| 49 | +fn ui_metadata(sections: &[SectionDecl]) -> toml::Table { |
| 50 | + let definitions = [ |
| 51 | + ("common", "常用", "Common"), |
| 52 | + ("gameplay", "游戏", "Gameplay"), |
| 53 | + ("display", "显示", "Display"), |
| 54 | + ("audio", "音频", "Audio"), |
| 55 | + ("network", "网络", "Network"), |
| 56 | + ("io", "IO", "IO"), |
| 57 | + ("compatibility", "兼容", "Compatibility"), |
| 58 | + ]; |
| 59 | + let groups = definitions |
| 60 | + .into_iter() |
| 61 | + .filter_map(|(id, label, label_en)| { |
| 62 | + let members = sections |
| 63 | + .iter() |
| 64 | + .filter(|section| { |
| 65 | + should_export(section) |
| 66 | + && section |
| 67 | + .group |
| 68 | + .as_ref() |
| 69 | + .map_or(id == "common", |group| group.value() == id) |
| 70 | + }) |
| 71 | + .map(|section| toml::Value::String(section.name.value())) |
| 72 | + .collect::<Vec<_>>(); |
| 73 | + if members.is_empty() { |
| 74 | + return None; |
| 75 | + } |
| 76 | + let mut group = toml::Table::new(); |
| 77 | + group.insert("id".to_owned(), toml::Value::String(id.to_owned())); |
| 78 | + group.insert("label".to_owned(), localized(label, Some(label_en))); |
| 79 | + group.insert("sections".to_owned(), toml::Value::Array(members)); |
| 80 | + Some(toml::Value::Table(group)) |
| 81 | + }) |
| 82 | + .collect(); |
| 83 | + let mut ui = toml::Table::new(); |
| 84 | + ui.insert("groups".to_owned(), toml::Value::Array(groups)); |
| 85 | + ui |
| 86 | +} |
| 87 | + |
| 88 | +fn section_value(section: &SectionDecl) -> Result<toml::Value, SchemaError> { |
| 89 | + let mut table = toml::Table::new(); |
| 90 | + table.insert("id".to_owned(), toml::Value::String(section.name.value())); |
| 91 | + table.insert( |
| 92 | + "default_enabled".to_owned(), |
| 93 | + toml::Value::Boolean(section.default_on.value), |
| 94 | + ); |
| 95 | + if section.always_enabled.value { |
| 96 | + table.insert("always_enabled".to_owned(), toml::Value::Boolean(true)); |
| 97 | + } |
| 98 | + if section.hidden.value { |
| 99 | + table.insert("hidden".to_owned(), toml::Value::Boolean(true)); |
| 100 | + } |
| 101 | + if section.community { |
| 102 | + table.insert("community".to_owned(), toml::Value::Boolean(true)); |
| 103 | + } |
| 104 | + table.insert( |
| 105 | + "label".to_owned(), |
| 106 | + localized(§ion.comment.value(), Some(§ion.name.value())), |
| 107 | + ); |
| 108 | + if let Some(description) = §ion.description { |
| 109 | + table.insert( |
| 110 | + "description".to_owned(), |
| 111 | + localized( |
| 112 | + &description.value(), |
| 113 | + section |
| 114 | + .description_en |
| 115 | + .as_ref() |
| 116 | + .map(LitStr::value) |
| 117 | + .as_deref(), |
| 118 | + ), |
| 119 | + ); |
| 120 | + } |
| 121 | + table.insert( |
| 122 | + "entries".to_owned(), |
| 123 | + toml::Value::Array( |
| 124 | + section |
| 125 | + .fields |
| 126 | + .iter() |
| 127 | + .map(field_value) |
| 128 | + .collect::<Result<Vec<_>, _>>()?, |
| 129 | + ), |
| 130 | + ); |
| 131 | + Ok(toml::Value::Table(table)) |
| 132 | +} |
| 133 | + |
| 134 | +fn field_value(field: &FieldDecl) -> Result<toml::Value, SchemaError> { |
| 135 | + let raw_key = field |
| 136 | + .key |
| 137 | + .as_ref() |
| 138 | + .map_or_else(|| field.name.to_string(), LitStr::value); |
| 139 | + let canonical_key = canonical_key(&raw_key); |
| 140 | + let mut table = toml::Table::new(); |
| 141 | + table.insert("key".to_owned(), toml::Value::String(canonical_key.clone())); |
| 142 | + table.insert( |
| 143 | + "type".to_owned(), |
| 144 | + toml::Value::String( |
| 145 | + field |
| 146 | + .schema_type |
| 147 | + .as_ref() |
| 148 | + .map_or_else(|| schema_type(&field.value_type), |value| Ok(value.value()))?, |
| 149 | + ), |
| 150 | + ); |
| 151 | + table.insert( |
| 152 | + "default".to_owned(), |
| 153 | + expression_value(field.schema_default.as_ref().unwrap_or(&field.default))?, |
| 154 | + ); |
| 155 | + table.insert( |
| 156 | + "emit_default".to_owned(), |
| 157 | + toml::Value::Boolean(field.emit_default), |
| 158 | + ); |
| 159 | + table.insert( |
| 160 | + "label".to_owned(), |
| 161 | + localized(&field.comment.value(), Some(&canonical_key)), |
| 162 | + ); |
| 163 | + if let Some(description) = &field.description { |
| 164 | + table.insert( |
| 165 | + "description".to_owned(), |
| 166 | + localized( |
| 167 | + &description.value(), |
| 168 | + field.description_en.as_ref().map(LitStr::value).as_deref(), |
| 169 | + ), |
| 170 | + ); |
| 171 | + } |
| 172 | + if field.comment.value().is_empty() { |
| 173 | + table.insert("emit_comment".to_owned(), toml::Value::Boolean(false)); |
| 174 | + } |
| 175 | + if let Some(min) = &field.min { |
| 176 | + table.insert("min".to_owned(), expression_value(min)?); |
| 177 | + } |
| 178 | + if let Some(max) = &field.max { |
| 179 | + table.insert("max".to_owned(), expression_value(max)?); |
| 180 | + } |
| 181 | + if !field.options.is_empty() { |
| 182 | + table.insert( |
| 183 | + "options".to_owned(), |
| 184 | + toml::Value::Array( |
| 185 | + field |
| 186 | + .options |
| 187 | + .iter() |
| 188 | + .map(expression_value) |
| 189 | + .collect::<Result<Vec<_>, _>>()? |
| 190 | + .into_iter() |
| 191 | + .map(option_value) |
| 192 | + .collect(), |
| 193 | + ), |
| 194 | + ); |
| 195 | + } |
| 196 | + Ok(toml::Value::Table(table)) |
| 197 | +} |
| 198 | + |
| 199 | +fn option_value(value: toml::Value) -> toml::Value { |
| 200 | + let label = match &value { |
| 201 | + toml::Value::String(value) => value.clone(), |
| 202 | + toml::Value::Integer(value) => value.to_string(), |
| 203 | + _ => String::new(), |
| 204 | + }; |
| 205 | + let mut option = toml::Table::new(); |
| 206 | + option.insert("value".to_owned(), value); |
| 207 | + option.insert("label".to_owned(), localized(&label, Some(&label))); |
| 208 | + toml::Value::Table(option) |
| 209 | +} |
| 210 | + |
| 211 | +fn localized(value: &str, en: Option<&str>) -> toml::Value { |
| 212 | + let mut label = toml::Table::new(); |
| 213 | + label.insert("zh".to_owned(), toml::Value::String(value.to_owned())); |
| 214 | + label.insert( |
| 215 | + "en".to_owned(), |
| 216 | + toml::Value::String(en.unwrap_or(value).to_owned()), |
| 217 | + ); |
| 218 | + toml::Value::Table(label) |
| 219 | +} |
| 220 | + |
| 221 | +fn should_export(section: &SectionDecl) -> bool { |
| 222 | + section.export.unwrap_or(!section.hidden.value) |
| 223 | +} |
0 commit comments