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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use super::{BuildError, EmptyToNone, EnumeratedValue, SvdError, ValidateLevel};
use std::borrow::Cow;
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(rename_all = "camelCase")
)]
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct DimElement {
pub dim: u32,
pub dim_increment: u32,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub dim_index: Option<Vec<String>>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub dim_name: Option<String>,
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub dim_array_index: Option<DimArrayIndex>,
}
#[cfg_attr(
feature = "serde",
derive(serde::Deserialize, serde::Serialize),
serde(rename_all = "camelCase")
)]
#[derive(Clone, Debug, PartialEq)]
pub struct DimArrayIndex {
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub header_enum_name: Option<String>,
pub values: Vec<EnumeratedValue>,
}
#[derive(Clone, Debug, Default, PartialEq)]
pub struct DimElementBuilder {
dim: Option<u32>,
dim_increment: Option<u32>,
dim_index: Option<Vec<String>>,
dim_name: Option<String>,
dim_array_index: Option<DimArrayIndex>,
}
impl From<DimElement> for DimElementBuilder {
fn from(d: DimElement) -> Self {
Self {
dim: Some(d.dim),
dim_increment: Some(d.dim_increment),
dim_index: d.dim_index,
dim_name: d.dim_name,
dim_array_index: d.dim_array_index,
}
}
}
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 dim_name(mut self, value: Option<String>) -> Self {
self.dim_name = value;
self
}
pub fn dim_array_index(mut self, value: Option<DimArrayIndex>) -> Self {
self.dim_array_index = value;
self
}
pub fn build(self, lvl: ValidateLevel) -> Result<DimElement, SvdError> {
let mut de = 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.empty_to_none(),
dim_name: self.dim_name.empty_to_none(),
dim_array_index: self.dim_array_index,
};
if !lvl.is_disabled() {
de.validate(lvl)?;
}
Ok(de)
}
}
impl DimElement {
pub fn builder() -> DimElementBuilder {
DimElementBuilder::default()
}
pub fn modify_from(
&mut self,
builder: DimElementBuilder,
lvl: ValidateLevel,
) -> Result<(), SvdError> {
if let Some(dim) = builder.dim {
self.dim = dim;
}
if let Some(dim_increment) = builder.dim_increment {
self.dim_increment = dim_increment;
}
if builder.dim_index.is_some() {
self.dim_index = builder.dim_index.empty_to_none();
}
if builder.dim_name.is_some() {
self.dim_name = builder.dim_name.empty_to_none();
}
if builder.dim_array_index.is_some() {
self.dim_array_index = builder.dim_array_index;
}
if !lvl.is_disabled() {
self.validate(lvl)
} else {
Ok(())
}
}
pub fn validate(&mut self, _lvl: ValidateLevel) -> Result<(), SvdError> {
Ok(())
}
pub fn indexes(&self) -> Indexes {
Indexes {
i: 0,
dim: self.dim,
dim_index: &self.dim_index,
}
}
}
pub struct Indexes<'a> {
i: u32,
dim: u32,
dim_index: &'a Option<Vec<String>>,
}
impl<'a> Iterator for Indexes<'a> {
type Item = Cow<'a, str>;
fn next(&mut self) -> Option<Self::Item> {
if self.i == self.dim {
return None;
}
let i = self.i;
self.i += 1;
if let Some(index) = self.dim_index.as_ref() {
Some(index[i as usize].as_str().into())
} else {
Some(i.to_string().into())
}
}
}