moduforge_core/model/node.rs
1use super::attrs::Attrs;
2use super::mark::Mark;
3use super::types::NodeId;
4use serde::{Deserialize, Serialize};
5/// 基础节点定义,任何数据都可以认为是节点
6///
7/// # 属性
8///
9/// * `id` - 节点唯一标识符
10/// * `type` - 节点类型
11/// * `attrs` - 节点属性,一般用于元数据的存储
12/// * `content` - 子节点列表
13/// * `marks` - 节点标记列表
14///
15/// # 示例
16///
17/// ```
18/// use moduforge_rs::model::node::Node;
19/// use moduforge_rs::model::attrs::Attrs;
20///
21/// let node = Node::new(
22/// "node1",
23/// "paragraph".to_string(),
24/// Attrs::default(),
25/// vec![],
26/// vec![],
27/// );
28/// ```
29
30#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
31pub struct Node {
32 #[serde(rename = "i")]
33 pub id: NodeId,
34 #[serde(rename = "t")]
35 pub r#type: String,
36 #[serde(rename = "a")]
37 pub attrs: Attrs,
38 #[serde(rename = "c")]
39 pub content: im::Vector<NodeId>, // 使用im::Vector替代Arc<Vec>
40 #[serde(rename = "m")]
41 pub marks: im::Vector<Mark>,
42}
43unsafe impl Send for Node {}
44unsafe impl Sync for Node {}
45
46impl Node {
47 /// 创建一个新的节点实例
48 ///
49 /// # 参数
50 ///
51 /// * `id` - 节点ID,字符串引用
52 /// * `type` - 节点类型
53 /// * `attrs` - 节点属性
54 /// * `content` - 子节点ID列表
55 /// * `marks` - 节点标记列表
56 ///
57 /// # 返回值
58 ///
59 /// 返回一个新的 `Node` 实例
60 pub fn new(
61 id: &str, // 接受字符串引用
62 r#type: String,
63 attrs: Attrs,
64 content: Vec<NodeId>,
65 marks: Vec<Mark>,
66 ) -> Self {
67 Node {
68 id: id.into(), // 转换为Arc<str>
69 r#type,
70 attrs,
71 content: content.into(),
72 marks: marks.into(),
73 }
74 }
75 /// 获取子节点数量
76 ///
77 /// # 返回值
78 ///
79 /// 返回节点包含的子节点数量
80 pub fn child_count(&self) -> usize {
81 self.content.len()
82 }
83}