mf_model/
schema.rs

1use crate::error::error_helpers::schema_error;
2use crate::error::PoolResult;
3
4use super::attrs::Attrs;
5use super::content::ContentMatch;
6use super::mark_definition::{MarkDefinition, MarkSpec};
7use super::node_definition::{NodeDefinition, NodeSpec};
8use crate::node_factory::NodeFactory;
9use serde::Serialize;
10use serde_json::Value;
11use std::any::Any;
12use std::collections::HashMap;
13use std::sync::{Arc, Mutex};
14/// 属性定义结构体
15/// 用于定义节点或标记的属性特征
16#[derive(Clone, PartialEq, Eq, Hash, Debug, Serialize)]
17pub struct Attribute {
18    pub has_default: bool,
19    pub default: Option<Value>,
20}
21
22impl Attribute {
23    /// 从 AttributeSpec 创建新的 Attribute 实例
24    pub(crate) fn new(options: AttributeSpec) -> Self {
25        Attribute {
26            has_default: options.default.is_some(),
27            default: options.default,
28        }
29    }
30    /// 检查属性是否为必需的
31    /// 如果没有默认值,则属性为必需
32    pub fn is_required(&self) -> bool {
33        !self.has_default
34    }
35}
36/// Schema 结构体定义
37/// 用于管理文档模型的整体结构,包括节点和标记的类型定义
38#[derive(Clone, Debug)]
39pub struct Schema {
40    /// Schema 的规范定义
41    pub spec: SchemaSpec,
42    /// 顶级节点类型
43    pub top_node_type: Option<NodeDefinition>,
44    /// 全局缓存
45    pub cached: Arc<Mutex<HashMap<String, Arc<dyn Any + Send + Sync>>>>,
46    /// 节点类型映射表
47    pub nodes: HashMap<String, NodeDefinition>,
48    /// 标记类型映射表
49    pub marks: HashMap<String, MarkDefinition>,
50}
51impl PartialEq for Schema {
52    fn eq(
53        &self,
54        other: &Self,
55    ) -> bool {
56        self.spec == other.spec
57            && self.top_node_type == other.top_node_type
58            && self.nodes == other.nodes
59            && self.marks == other.marks
60    }
61}
62impl Eq for Schema {}
63impl Schema {
64    /// 创建新的 Schema 实例
65    pub fn new(spec: SchemaSpec) -> Self {
66        let mut instance_spec = SchemaSpec {
67            nodes: HashMap::new(),
68            marks: HashMap::new(),
69            top_node: spec.top_node,
70        };
71        // 复制 spec 属性
72        for (key, value) in spec.nodes {
73            instance_spec.nodes.insert(key, value);
74        }
75        for (key, value) in spec.marks {
76            instance_spec.marks.insert(key, value);
77        }
78        Schema {
79            spec: instance_spec,
80            top_node_type: None,
81            cached: Arc::new(Mutex::new(HashMap::new())),
82            nodes: HashMap::new(),
83            marks: HashMap::new(),
84        }
85    }
86    pub fn factory(&self) -> NodeFactory<'_> {
87        NodeFactory::new(self)
88    }
89    /// 编译 Schema 定义
90    /// 处理节点和标记的定义,建立它们之间的关系
91    pub fn compile(instance_spec: SchemaSpec) -> PoolResult<Schema> {
92        let mut schema: Schema = Schema::new(instance_spec);
93        let nodes: HashMap<String, NodeDefinition> =
94            NodeDefinition::compile(schema.spec.nodes.clone());
95        let marks = MarkDefinition::compile(schema.spec.marks.clone());
96        let mut content_expr_cache = HashMap::new();
97        let mut updated_nodes = HashMap::new();
98        for (prop, type_) in &nodes {
99            if marks.contains_key(prop) {
100                return Err(schema_error(&format!(
101                    "{prop} 不能既是节点又是标记"
102                )));
103            }
104
105            let content_expr = type_.spec.content.as_deref().unwrap_or("");
106            let mark_expr = type_.spec.marks.as_deref();
107
108            let content_expr_string = content_expr.to_string();
109            let content_match = content_expr_cache
110                .entry(content_expr_string.clone())
111                .or_insert_with(|| {
112                    ContentMatch::parse(content_expr_string, &nodes)
113                })
114                .clone();
115
116            let mark_set = match mark_expr {
117                Some("_") => None,
118                Some(expr) => {
119                    let marks_result =
120                        gather_marks(&marks, expr.split_whitespace().collect());
121                    match marks_result {
122                        Ok(marks) => Some(marks.into_iter().cloned().collect()), // Convert Vec<&MarkType> to Vec<MarkType>
123                        Err(e) => return Err(schema_error(&e)),
124                    }
125                },
126                None => None,
127            };
128
129            let mut node = type_.clone();
130            node.content_match = Some(content_match);
131            node.mark_set = mark_set;
132            updated_nodes.insert(prop.clone(), node);
133        }
134        schema.nodes = updated_nodes;
135        schema.marks = marks;
136        schema.top_node_type = match schema.nodes.get(
137            &schema.spec.top_node.clone().unwrap_or_else(|| "doc".to_string()),
138        ) {
139            Some(node) => Some(node.clone()),
140            None => {
141                return Err(schema_error("未找到顶级节点类型定义"));
142            },
143        };
144
145        Ok(schema)
146    }
147}
148/// Schema 规范定义
149/// 包含节点和标记的原始定义信息
150#[derive(Clone, PartialEq, Eq, Debug)]
151pub struct SchemaSpec {
152    pub nodes: HashMap<String, NodeSpec>,
153    pub marks: HashMap<String, MarkSpec>,
154    pub top_node: Option<String>,
155}
156
157// 其他辅助函数...
158/// 获取属性的默认值映射
159/// 如果所有属性都有默认值,返回包含所有默认值的映射
160/// 如果任一属性没有默认值,返回 None
161pub fn default_attrs(
162    attrs: &HashMap<String, Attribute>
163) -> Option<HashMap<String, Value>> {
164    let mut defaults = HashMap::new();
165
166    for (attr_name, attr) in attrs {
167        if let Some(default) = &attr.default {
168            defaults.insert(attr_name.clone(), default.clone());
169        } else {
170            return None;
171        }
172    }
173
174    Some(defaults)
175}
176/// 属性规范定义
177#[derive(Clone, PartialEq, Debug, Eq, Hash, Serialize)]
178pub struct AttributeSpec {
179    /// 属性的默认值
180    pub default: Option<Value>,
181}
182/// 收集标记类型
183/// 根据给定的标记名称列表,收集对应的标记类型
184fn gather_marks<'a>(
185    marks_map: &'a HashMap<String, MarkDefinition>,
186    marks: Vec<&'a str>,
187) -> Result<Vec<&'a MarkDefinition>, String> {
188    let mut found = Vec::new();
189
190    for name in marks {
191        if let Some(mark) = marks_map.get(name) {
192            found.push(mark);
193        } else if name == "_" {
194            // "_" 表示所有标记类型都被允许
195            found.extend(marks_map.values());
196        } else {
197            // 尝试通过组名匹配标记
198            let mut matched = false;
199            for mark_ref in marks_map.values() {
200                if mark_ref.spec.group.as_ref().is_some_and(|group| {
201                    group.split_whitespace().any(|g| g == name)
202                }) {
203                    found.push(mark_ref);
204                    matched = true;
205                    break;
206                }
207            }
208            if !matched {
209                return Err(format!("未知的标记类型: '{name}'"));
210            }
211        }
212    }
213    Ok(found)
214}
215/// 计算属性值
216/// 根据属性定义和提供的值计算最终的属性值
217pub fn compute_attrs(
218    attrs: &HashMap<String, Attribute>,
219    value: Option<&HashMap<String, Value>>,
220) -> Attrs {
221    let mut built = Attrs::default();
222
223    for (name, attr) in attrs {
224        let given = value.and_then(|v| v.get(name));
225
226        let given = match given {
227            Some(val) => val.clone(),
228            None => {
229                if attr.has_default {
230                    attr.default.clone().unwrap_or_else(|| {
231                        panic!("没有为属性提供默认值 {name}")
232                    })
233                } else {
234                    Value::Null
235                }
236            },
237        };
238
239        built[name] = given;
240    }
241
242    built
243}