|
| 1 | +use std::collections::{BTreeMap, BTreeSet}; |
| 2 | + |
| 3 | +use serde::Serialize; |
| 4 | + |
| 5 | +use super::{DslError, Element, parser::attrs}; |
| 6 | + |
| 7 | +#[derive(Debug, Clone, Serialize)] |
| 8 | +pub struct ConfigDescription { |
| 9 | + pub title: String, |
| 10 | + pub description: String, |
| 11 | + pub configs: Vec<DescriptionConfig>, |
| 12 | +} |
| 13 | + |
| 14 | +#[derive(Debug, Clone, Serialize)] |
| 15 | +pub struct DescriptionConfig { |
| 16 | + pub name: String, |
| 17 | + pub label: String, |
| 18 | + pub filename: String, |
| 19 | + pub selectors: Vec<DescriptionSelector>, |
| 20 | + pub lines: Vec<DescriptionLine>, |
| 21 | +} |
| 22 | + |
| 23 | +#[derive(Debug, Clone, Serialize)] |
| 24 | +pub struct DescriptionSelector { |
| 25 | + pub name: String, |
| 26 | + pub label: String, |
| 27 | + pub default: String, |
| 28 | + pub choices: Vec<(String, String)>, |
| 29 | +} |
| 30 | + |
| 31 | +#[derive(Debug, Clone, Serialize)] |
| 32 | +pub struct DescriptionLine { |
| 33 | + pub yaml: String, |
| 34 | + pub description: String, |
| 35 | + pub conditions: Vec<(String, String)>, |
| 36 | +} |
| 37 | + |
| 38 | +fn identifier<'a>(node: &'a Element, attr: &str) -> Result<&'a str, DslError> { |
| 39 | + let value = node.required(attr)?; |
| 40 | + if value.is_empty() |
| 41 | + || !value.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-') |
| 42 | + || value.as_bytes()[0].is_ascii_digit() |
| 43 | + { |
| 44 | + return Err(node.error("名称必须是静态标识符")); |
| 45 | + } |
| 46 | + Ok(value) |
| 47 | +} |
| 48 | + |
| 49 | +pub(super) fn parse(node: Option<&Element>) -> Result<Option<ConfigDescription>, DslError> { |
| 50 | + let Some(node) = node else { return Ok(None) }; |
| 51 | + attrs(node, &["title", "description"])?; |
| 52 | + let mut configs = Vec::new(); |
| 53 | + let mut config_names = BTreeSet::new(); |
| 54 | + for config in &node.children { |
| 55 | + if config.tag != "config" { |
| 56 | + return Err(config.error("config-desc 只接受 config")); |
| 57 | + } |
| 58 | + attrs(config, &["name", "label", "filename"])?; |
| 59 | + let name = identifier(config, "name")?; |
| 60 | + if !config_names.insert(name.to_owned()) { |
| 61 | + return Err(config.error("重复配置名称")); |
| 62 | + } |
| 63 | + let mut selectors = Vec::new(); |
| 64 | + let mut selector_names = BTreeSet::new(); |
| 65 | + let mut lines = Vec::new(); |
| 66 | + for child in &config.children { |
| 67 | + match child.tag.as_str() { |
| 68 | + "selector" => { |
| 69 | + attrs(child, &["name", "label", "default"])?; |
| 70 | + let selector_name = identifier(child, "name")?; |
| 71 | + if !selector_names.insert(selector_name.to_owned()) { |
| 72 | + return Err(child.error("重复选择器名称")); |
| 73 | + } |
| 74 | + let mut choices = Vec::new(); |
| 75 | + let mut values = BTreeSet::new(); |
| 76 | + for choice in &child.children { |
| 77 | + if choice.tag != "choice" || !choice.children.is_empty() { |
| 78 | + return Err(choice.error("selector 只接受空 choice")); |
| 79 | + } |
| 80 | + attrs(choice, &["value", "label"])?; |
| 81 | + let value = identifier(choice, "value")?; |
| 82 | + if !values.insert(value.to_owned()) { |
| 83 | + return Err(choice.error("重复选择值")); |
| 84 | + } |
| 85 | + choices.push((value.into(), choice.required("label")?.into())); |
| 86 | + } |
| 87 | + let default = child.required("default")?; |
| 88 | + if choices.is_empty() || !values.contains(default) { |
| 89 | + return Err(child.error("选择器默认值必须属于非空选项")); |
| 90 | + } |
| 91 | + selectors.push(DescriptionSelector { |
| 92 | + name: selector_name.into(), |
| 93 | + label: child.required("label")?.into(), |
| 94 | + default: default.into(), |
| 95 | + choices, |
| 96 | + }); |
| 97 | + } |
| 98 | + "line" => { |
| 99 | + attrs(child, &["yaml", "description", "when"])?; |
| 100 | + if !child.children.is_empty() { |
| 101 | + return Err(child.error("line 不接受子元素")); |
| 102 | + } |
| 103 | + lines.push(DescriptionLine { |
| 104 | + yaml: child.required("yaml")?.into(), |
| 105 | + description: child.required("description")?.into(), |
| 106 | + conditions: parse_conditions(child)?, |
| 107 | + }); |
| 108 | + } |
| 109 | + _ => return Err(child.error("config 只接受 selector 或 line")), |
| 110 | + } |
| 111 | + } |
| 112 | + if lines.is_empty() { |
| 113 | + return Err(config.error("配置说明至少需要一行 YAML")); |
| 114 | + } |
| 115 | + let selector_map: BTreeMap<_, _> = selectors.iter().map(|s| (s.name.as_str(), s)).collect(); |
| 116 | + for line in &lines { |
| 117 | + for (selector, value) in &line.conditions { |
| 118 | + let Some(selector) = selector_map.get(selector.as_str()) else { |
| 119 | + return Err(config.error("YAML 行引用了不存在的选择器")); |
| 120 | + }; |
| 121 | + if !selector.choices.iter().any(|(candidate, _)| candidate == value) { |
| 122 | + return Err(config.error("YAML 行引用了不存在的选择值")); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + configs.push(DescriptionConfig { |
| 127 | + name: name.into(), |
| 128 | + label: config.required("label")?.into(), |
| 129 | + filename: config.required("filename")?.into(), |
| 130 | + selectors, |
| 131 | + lines, |
| 132 | + }); |
| 133 | + } |
| 134 | + if configs.is_empty() { |
| 135 | + return Err(node.error("config-desc 至少需要一个 config")); |
| 136 | + } |
| 137 | + Ok(Some(ConfigDescription { |
| 138 | + title: node.required("title")?.into(), |
| 139 | + description: node.attr("description").unwrap_or_default().into(), |
| 140 | + configs, |
| 141 | + })) |
| 142 | +} |
| 143 | + |
| 144 | +fn parse_conditions(node: &Element) -> Result<Vec<(String, String)>, DslError> { |
| 145 | + let mut conditions = Vec::new(); |
| 146 | + let mut names = BTreeSet::new(); |
| 147 | + for condition in node.attr("when").unwrap_or_default().split(',').filter(|s| !s.is_empty()) { |
| 148 | + let Some((name, value)) = condition.split_once('=') else { |
| 149 | + return Err(node.error("when 必须使用 selector=value,并以逗号分隔")); |
| 150 | + }; |
| 151 | + if !names.insert(name) || name.is_empty() || value.is_empty() { |
| 152 | + return Err(node.error("when 含有空值或重复选择器")); |
| 153 | + } |
| 154 | + conditions.push((name.into(), value.into())); |
| 155 | + } |
| 156 | + Ok(conditions) |
| 157 | +} |
0 commit comments