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
use super::node::Node;
use serde::{Serialize, Serializer};
use std::collections::{BTreeMap, HashMap};
use std::default::Default;
use std::result::Result;
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum ElementVariant {
Normal,
Void,
}
pub type Attributes = HashMap<String, Option<String>>;
#[derive(Debug, Clone, Serialize, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Element {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
pub name: String,
pub variant: ElementVariant,
#[serde(skip_serializing_if = "HashMap::is_empty")]
#[serde(serialize_with = "ordered_map")]
pub attributes: Attributes,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub classes: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub children: Vec<Node>,
}
impl Default for Element {
fn default() -> Self {
Self {
id: None,
name: "".to_string(),
variant: ElementVariant::Void,
classes: vec![],
attributes: HashMap::new(),
children: vec![],
}
}
}
fn ordered_map<S: Serializer>(value: &Attributes, serializer: S) -> Result<S::Ok, S::Error> {
let ordered: BTreeMap<_, _> = value.iter().collect();
ordered.serialize(serializer)
}