1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
pub mod definition;
pub mod visitor;
pub mod xml_writer;

use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::BTreeMap;

pub enum DataValue {
    String { kind: String, value: String },
    Image(Image),
    Json(Value),
}

#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum Image {
    Png(String),
    Svg(String),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Node {
    Plain(String),
    Compound(Compound),
    Script(Script),
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Script {
    pub id: String,
    pub src: String,
    pub elements: Vec<Vec<Node>>,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Compound {
    pub type_id: String,
    pub id: Option<String>,
    pub attributes: Vec<(Option<String>, Attribute)>,
    pub children: Vec<Node>,
}

impl Compound {
    pub fn new<S: Into<String>, B: IntoIterator<Item = (Option<String>, Attribute)>>(
        type_id: S,
        id: Option<&str>,
        attributes: B,
        children: Vec<Node>,
    ) -> Self {
        Self {
            type_id: type_id.into(),
            id: id.map(String::from),
            attributes: attributes.into_iter().collect(),
            children,
        }
    }

    pub fn new_with_children<S: Into<String>>(
        type_id: S,
        id: Option<&str>,
        children: Vec<Node>,
    ) -> Self {
        Self::new(type_id, id, BTreeMap::new(), children)
    }

    pub fn new_with_attributes<
        S: Into<String>,
        B: IntoIterator<Item = (Option<String>, Attribute)>,
    >(
        type_id: S,
        id: Option<&str>,
        attributes: B,
    ) -> Self {
        Self {
            type_id: type_id.into(),
            id: id.map(String::from),
            attributes: attributes.into_iter().collect(),
            children: vec![],
        }
    }

    pub fn new_empty<S: Into<String>>(type_id: S, id: Option<&str>) -> Self {
        Self {
            type_id: type_id.into(),
            id: id.map(String::from),
            attributes: Vec::new(),
            children: vec![],
        }
    }
}

impl Node {
    pub fn get_compound(&self) -> Option<&Compound> {
        if let Node::Compound(c) = self {
            Some(c)
        } else {
            None
        }
    }

    pub fn get_plain(&self) -> Option<&String> {
        if let Node::Plain(s) = self {
            Some(s)
        } else {
            None
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub enum Attribute {
    Int(i64),
    Float(f64),
    String(String),
    Enum(String),
    Compound(Vec<Node>),
    Flag,
}

impl Attribute {
    pub fn get_string(&self) -> &str {
        if let Attribute::String(s) = self {
            s
        } else {
            panic!()
        }
    }
}

// #[derive(Deserialize)]
// pub struct NodeTypeDef {
//     pub type_id: String,
//     pub attributes: Option<Vec<AttributeDef>>,
//     pub children: Option<Vec<NodeChildSpec>>,
// }
//
// #[derive(Deserialize)]
// pub struct NodeChildSpec {
//     pub type_: ChildType,
//     pub rule: ChildRule,
// }
//
// #[derive(Deserialize)]
// pub enum ChildType {
//     Any,
//     Is(String),
//     OneOf(Vec<ChildType>),
// }
//
// #[derive(Deserialize)]
// pub enum ChildRule {
//     One,
//     OneOrMany,
//     ZeroOrMany,
//     ZeroOrOne,
//     Exactly(usize),
// }
//
// #[derive(Deserialize)]
// pub struct AttributeDef {
//     pub name: String,
//     pub optional: bool,
//     pub data_type: DataType,
// }
//
// #[derive(Deserialize)]
// pub enum DataType {
//     Int,
//     Float,
//     String,
//     Enum(Vec<String>),
//     Flag,
// }