mf_core/
node.rs

1use std::collections::HashMap;
2
3use mf_model::{node_type::NodeSpec, schema::AttributeSpec};
4use serde_json::Value;
5#[derive(Clone, PartialEq, Debug, Eq, Default)]
6pub struct Node {
7    pub name: String,
8    pub r#type: NodeSpec,
9    pub top_node: bool,
10}
11
12impl Node {
13    pub fn create(
14        name: &str,
15        spec: NodeSpec,
16    ) -> Node {
17        Node { name: name.to_string(), r#type: spec, top_node: false }
18    }
19    pub fn set_name(
20        &mut self,
21        name: &str,
22    ) -> &mut Self {
23        self.name = name.to_string();
24        self
25    }
26    pub fn set_top_node(&mut self) -> &mut Self {
27        self.top_node = true;
28        self
29    }
30    pub fn is_top_node(&self) -> bool {
31        self.top_node
32    }
33    pub fn get_name(&self) -> &str {
34        &self.name
35    }
36    pub fn set_content(
37        &mut self,
38        content: &str,
39    ) -> &mut Self {
40        self.r#type.content = Some(content.to_string());
41        self
42    }
43
44    pub fn set_marks(
45        &mut self,
46        marks: String,
47    ) -> &mut Self {
48        self.r#type.marks = Some(marks);
49        self
50    }
51
52    pub fn set_attrs(
53        &mut self,
54        attrs: HashMap<String, AttributeSpec>,
55    ) -> &mut Self {
56        self.r#type.attrs = Some(attrs);
57        self
58    }
59    pub fn set_attr(
60        &mut self,
61        name: &str,
62        default: Option<Value>,
63    ) -> &mut Self {
64        match &mut self.r#type.attrs {
65            Some(map) => {
66                map.insert(name.to_string(), AttributeSpec { default });
67            },
68            None => {
69                let mut new_map = HashMap::new();
70                new_map.insert(name.to_string(), AttributeSpec { default });
71                self.r#type.attrs = Some(new_map);
72            },
73        }
74        self
75    }
76    pub fn set_desc(
77        &mut self,
78        desc: &str,
79    ) -> &mut Self {
80        self.r#type.desc = Some(desc.to_string());
81        self
82    }
83}