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
use xmltree::Element;
use crate::types::{parse_optional, DimIndex, Parse};
use crate::elementext::ElementExt;
use crate::encode::Encode;
use crate::new_element;
use crate::error::*;
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct DimElement {
pub dim: u32,
pub dim_increment: u32,
#[cfg_attr(feature = "serde", serde(default))]
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
pub dim_index: Option<Vec<String>>,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct DimElementBuilder {
dim: Option<u32>,
dim_increment: Option<u32>,
dim_index: Option<Vec<String>>,
}
impl DimElementBuilder {
pub fn dim(mut self, value: u32) -> Self {
self.dim = Some(value);
self
}
pub fn dim_increment(mut self, value: u32) -> Self {
self.dim_increment = Some(value);
self
}
pub fn dim_index(mut self, value: Option<Vec<String>>) -> Self {
self.dim_index = value;
self
}
pub fn build(self) -> Result<DimElement> {
Ok(DimElement {
dim: self
.dim
.ok_or_else(|| BuildError::Uninitialized("dim".to_string()))?,
dim_increment: self
.dim_increment
.ok_or_else(|| BuildError::Uninitialized("dim_increment".to_string()))?,
dim_index: self.dim_index,
})
}
}
impl Parse for DimElement {
type Object = Self;
type Error = anyhow::Error;
fn parse(tree: &Element) -> Result<Self> {
DimElementBuilder::default()
.dim(tree.get_child_u32("dim")?)
.dim_increment(tree.get_child_u32("dimIncrement")?)
.dim_index(parse_optional::<DimIndex>("dimIndex", tree)?)
.build()
}
}
impl Encode for DimElement {
type Error = anyhow::Error;
fn encode(&self) -> Result<Element> {
let mut e = new_element("dimElement", None);
e.children
.push(new_element("dim", Some(format!("{}", self.dim))));
e.children.push(new_element(
"dimIncrement",
Some(format!("{}", self.dim_increment)),
));
if let Some(di) = &self.dim_index {
e.children.push(new_element("dimIndex", Some(di.join(","))));
}
Ok(e)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::run_test;
#[test]
fn decode_encode() {
let tests = vec![(
DimElementBuilder::default()
.dim(100)
.dim_increment(4)
.dim_index(Some(vec!["10".to_string(), "20".to_string()]))
.build()
.unwrap(),
"<dimElement>
<dim>100</dim>
<dimIncrement>4</dimIncrement>
<dimIndex>10,20</dimIndex>
</dimElement>
",
)];
run_test::<DimElement>(&tests[..]);
}
}