cdoc_base/node/
mod.rs

1pub mod definition;
2pub mod visitor;
3pub mod xml_writer;
4
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7use std::collections::BTreeMap;
8
9pub enum DataValue {
10    String { kind: String, value: String },
11    Image(Image),
12    Json(Value),
13}
14
15#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
16pub enum Image {
17    Png(String),
18    Svg(String),
19}
20
21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
22pub enum Node {
23    Plain(String),
24    Compound(Compound),
25    Script(Script),
26}
27
28#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
29pub struct Script {
30    pub id: String,
31    pub src: String,
32    pub elements: Vec<Vec<Node>>,
33}
34
35#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
36pub struct Compound {
37    pub type_id: String,
38    pub id: Option<String>,
39    pub attributes: Vec<(Option<String>, Attribute)>,
40    pub children: Vec<Node>,
41}
42
43impl Compound {
44    pub fn new<S: Into<String>, B: IntoIterator<Item = (Option<String>, Attribute)>>(
45        type_id: S,
46        id: Option<&str>,
47        attributes: B,
48        children: Vec<Node>,
49    ) -> Self {
50        Self {
51            type_id: type_id.into(),
52            id: id.map(String::from),
53            attributes: attributes.into_iter().collect(),
54            children,
55        }
56    }
57
58    pub fn new_with_children<S: Into<String>>(
59        type_id: S,
60        id: Option<&str>,
61        children: Vec<Node>,
62    ) -> Self {
63        Self::new(type_id, id, BTreeMap::new(), children)
64    }
65
66    pub fn new_with_attributes<
67        S: Into<String>,
68        B: IntoIterator<Item = (Option<String>, Attribute)>,
69    >(
70        type_id: S,
71        id: Option<&str>,
72        attributes: B,
73    ) -> Self {
74        Self {
75            type_id: type_id.into(),
76            id: id.map(String::from),
77            attributes: attributes.into_iter().collect(),
78            children: vec![],
79        }
80    }
81
82    pub fn new_empty<S: Into<String>>(type_id: S, id: Option<&str>) -> Self {
83        Self {
84            type_id: type_id.into(),
85            id: id.map(String::from),
86            attributes: Vec::new(),
87            children: vec![],
88        }
89    }
90}
91
92impl Node {
93    pub fn get_compound(&self) -> Option<&Compound> {
94        if let Node::Compound(c) = self {
95            Some(c)
96        } else {
97            None
98        }
99    }
100
101    pub fn get_plain(&self) -> Option<&String> {
102        if let Node::Plain(s) = self {
103            Some(s)
104        } else {
105            None
106        }
107    }
108}
109
110#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
111pub enum Attribute {
112    Int(i64),
113    Float(f64),
114    String(String),
115    Enum(String),
116    Compound(Vec<Node>),
117    Flag,
118}
119
120impl Attribute {
121    pub fn get_string(&self) -> &str {
122        if let Attribute::String(s) = self {
123            s
124        } else {
125            panic!()
126        }
127    }
128}
129
130// #[derive(Deserialize)]
131// pub struct NodeTypeDef {
132//     pub type_id: String,
133//     pub attributes: Option<Vec<AttributeDef>>,
134//     pub children: Option<Vec<NodeChildSpec>>,
135// }
136//
137// #[derive(Deserialize)]
138// pub struct NodeChildSpec {
139//     pub type_: ChildType,
140//     pub rule: ChildRule,
141// }
142//
143// #[derive(Deserialize)]
144// pub enum ChildType {
145//     Any,
146//     Is(String),
147//     OneOf(Vec<ChildType>),
148// }
149//
150// #[derive(Deserialize)]
151// pub enum ChildRule {
152//     One,
153//     OneOrMany,
154//     ZeroOrMany,
155//     ZeroOrOne,
156//     Exactly(usize),
157// }
158//
159// #[derive(Deserialize)]
160// pub struct AttributeDef {
161//     pub name: String,
162//     pub optional: bool,
163//     pub data_type: DataType,
164// }
165//
166// #[derive(Deserialize)]
167// pub enum DataType {
168//     Int,
169//     Float,
170//     String,
171//     Enum(Vec<String>),
172//     Flag,
173// }