Skip to content

Commit f72c321

Browse files
authored
Merge pull request #251 from rust-embedded/validate
fix validate on ValidateLevel::Disabled
2 parents bd9210b + a60e177 commit f72c321

File tree

12 files changed

+145
-197
lines changed

12 files changed

+145
-197
lines changed

svd-rs/src/addressblock.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl AddressBlockBuilder {
105105
}
106106
/// Validate and build a [`AddressBlock`].
107107
pub fn build(self, lvl: ValidateLevel) -> Result<AddressBlock, SvdError> {
108-
let mut de = AddressBlock {
108+
let de = AddressBlock {
109109
offset: self
110110
.offset
111111
.ok_or_else(|| BuildError::Uninitialized("offset".to_string()))?,
@@ -117,9 +117,7 @@ impl AddressBlockBuilder {
117117
.ok_or_else(|| BuildError::Uninitialized("usage".to_string()))?,
118118
protection: self.protection,
119119
};
120-
if !lvl.is_disabled() {
121-
de.validate(lvl)?;
122-
}
120+
de.validate(lvl)?;
123121
Ok(de)
124122
}
125123
}
@@ -147,18 +145,14 @@ impl AddressBlock {
147145
if builder.protection.is_some() {
148146
self.protection = builder.protection;
149147
}
150-
if !lvl.is_disabled() {
151-
self.validate(lvl)
152-
} else {
153-
Ok(())
154-
}
148+
self.validate(lvl)
155149
}
156150
/// Validate the [`AddressBlock`].
157151
///
158152
/// # Notes
159153
///
160154
/// This doesn't do anything.
161-
pub fn validate(&mut self, _lvl: ValidateLevel) -> Result<(), SvdError> {
155+
pub fn validate(&self, _lvl: ValidateLevel) -> Result<(), SvdError> {
162156
Ok(())
163157
}
164158
}

svd-rs/src/cluster.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ impl ClusterInfoBuilder {
168168
}
169169
/// Validate and build a [`ClusterInfo`].
170170
pub fn build(self, lvl: ValidateLevel) -> Result<ClusterInfo, SvdError> {
171-
let mut cluster = ClusterInfo {
171+
let cluster = ClusterInfo {
172172
name: self
173173
.name
174174
.ok_or_else(|| BuildError::Uninitialized("name".to_string()))?,
@@ -184,9 +184,7 @@ impl ClusterInfoBuilder {
184184
.ok_or_else(|| BuildError::Uninitialized("children".to_string()))?,
185185
derived_from: self.derived_from,
186186
};
187-
if !lvl.is_disabled() {
188-
cluster.validate(lvl)?;
189-
}
187+
cluster.validate(lvl)?;
190188
Ok(cluster)
191189
}
192190
}
@@ -236,24 +234,22 @@ impl ClusterInfo {
236234
self.children = children;
237235
}
238236
}
239-
if !lvl.is_disabled() {
240-
self.validate(lvl)
241-
} else {
242-
Ok(())
243-
}
237+
self.validate(lvl)
244238
}
245239

246240
/// Validate the [`ClusterInfo`]
247-
pub fn validate(&mut self, lvl: ValidateLevel) -> Result<(), SvdError> {
248-
if lvl.is_strict() {
249-
super::check_dimable_name(&self.name, "name")?;
250-
}
251-
if let Some(name) = self.derived_from.as_ref() {
241+
pub fn validate(&self, lvl: ValidateLevel) -> Result<(), SvdError> {
242+
if !lvl.is_disabled() {
252243
if lvl.is_strict() {
253-
super::check_derived_name(name, "derivedFrom")?;
244+
super::check_dimable_name(&self.name, "name")?;
245+
}
246+
if let Some(name) = self.derived_from.as_ref() {
247+
if lvl.is_strict() {
248+
super::check_derived_name(name, "derivedFrom")?;
249+
}
250+
} else if self.children.is_empty() && lvl.is_strict() {
251+
return Err(Error::EmptyCluster.into());
254252
}
255-
} else if self.children.is_empty() && lvl.is_strict() {
256-
return Err(Error::EmptyCluster.into());
257253
}
258254
Ok(())
259255
}

svd-rs/src/cpu.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl CpuBuilder {
225225
}
226226
/// Validate and build a [`Cpu`].
227227
pub fn build(self, lvl: ValidateLevel) -> Result<Cpu, SvdError> {
228-
let mut cpu = Cpu {
228+
let cpu = Cpu {
229229
name: self
230230
.name
231231
.ok_or_else(|| BuildError::Uninitialized("name".to_string()))?,
@@ -257,9 +257,7 @@ impl CpuBuilder {
257257
device_num_interrupts: self.device_num_interrupts,
258258
sau_num_regions: self.sau_num_regions,
259259
};
260-
if !lvl.is_disabled() {
261-
cpu.validate(lvl)?;
262-
}
260+
cpu.validate(lvl)?;
263261
Ok(cpu)
264262
}
265263
}
@@ -319,14 +317,10 @@ impl Cpu {
319317
if builder.sau_num_regions.is_some() {
320318
self.sau_num_regions = builder.sau_num_regions;
321319
}
322-
if !lvl.is_disabled() {
323-
self.validate(lvl)
324-
} else {
325-
Ok(())
326-
}
320+
self.validate(lvl)
327321
}
328322
/// Validate the [`Cpu`]
329-
pub fn validate(&mut self, _lvl: ValidateLevel) -> Result<(), SvdError> {
323+
pub fn validate(&self, _lvl: ValidateLevel) -> Result<(), SvdError> {
330324
// TODO
331325
Ok(())
332326
}

svd-rs/src/device.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ impl DeviceBuilder {
255255
/// Validate and build a [`Device`].
256256
pub fn build(self, lvl: ValidateLevel) -> Result<Device, SvdError> {
257257
let schema_version = self.schema_version.unwrap_or_else(default_schema_version);
258-
let mut device = Device {
258+
let device = Device {
259259
vendor: self.vendor,
260260
vendor_id: self.vendor_id,
261261
name: self
@@ -304,9 +304,7 @@ impl DeviceBuilder {
304304
.unwrap_or_else(default_no_namespace_schema_location),
305305
schema_version,
306306
};
307-
if !lvl.is_disabled() {
308-
device.validate(lvl)?;
309-
}
307+
device.validate(lvl)?;
310308
Ok(device)
311309
}
312310
}
@@ -372,17 +370,15 @@ impl Device {
372370
if let Some(schema_version) = builder.schema_version {
373371
self.schema_version = schema_version;
374372
}
375-
if !lvl.is_disabled() {
376-
self.validate(lvl)
377-
} else {
378-
Ok(())
379-
}
373+
self.validate(lvl)
380374
}
381375
/// Validate the [`Device`]
382-
pub fn validate(&mut self, _lvl: ValidateLevel) -> Result<(), SvdError> {
383-
// TODO
384-
if self.peripherals.is_empty() {
385-
return Err(Error::EmptyDevice.into());
376+
pub fn validate(&self, lvl: ValidateLevel) -> Result<(), SvdError> {
377+
if !lvl.is_disabled() {
378+
// TODO
379+
if self.peripherals.is_empty() {
380+
return Err(Error::EmptyDevice.into());
381+
}
386382
}
387383
Ok(())
388384
}

svd-rs/src/dimelement.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl DimElementBuilder {
118118
}
119119
/// Validate and build a [`DimElement`].
120120
pub fn build(self, lvl: ValidateLevel) -> Result<DimElement, SvdError> {
121-
let mut de = DimElement {
121+
let de = DimElement {
122122
dim: self
123123
.dim
124124
.ok_or_else(|| BuildError::Uninitialized("dim".to_string()))?,
@@ -129,9 +129,7 @@ impl DimElementBuilder {
129129
dim_name: self.dim_name.empty_to_none(),
130130
dim_array_index: self.dim_array_index,
131131
};
132-
if !lvl.is_disabled() {
133-
de.validate(lvl)?;
134-
}
132+
de.validate(lvl)?;
135133
Ok(de)
136134
}
137135
}
@@ -211,18 +209,14 @@ impl DimElement {
211209
if builder.dim_array_index.is_some() {
212210
self.dim_array_index = builder.dim_array_index;
213211
}
214-
if !lvl.is_disabled() {
215-
self.validate(lvl)
216-
} else {
217-
Ok(())
218-
}
212+
self.validate(lvl)
219213
}
220214
/// Validate the [`DimElement`].
221215
///
222216
/// # Notes
223217
///
224218
/// This doesn't do anything.
225-
pub fn validate(&mut self, _lvl: ValidateLevel) -> Result<(), SvdError> {
219+
pub fn validate(&self, _lvl: ValidateLevel) -> Result<(), SvdError> {
226220
// TODO
227221
Ok(())
228222
}

svd-rs/src/enumeratedvalue.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,15 @@ impl EnumeratedValueBuilder {
8888
}
8989
/// Validate and build a [`EnumeratedValue`].
9090
pub fn build(self, lvl: ValidateLevel) -> Result<EnumeratedValue, SvdError> {
91-
let mut ev = EnumeratedValue {
91+
let ev = EnumeratedValue {
9292
name: self
9393
.name
9494
.ok_or_else(|| BuildError::Uninitialized("name".to_string()))?,
9595
description: self.description.empty_to_none(),
9696
value: self.value,
9797
is_default: self.is_default,
9898
};
99-
if !lvl.is_disabled() {
100-
ev.validate(lvl)?;
101-
}
99+
ev.validate(lvl)?;
102100
Ok(ev)
103101
}
104102
}
@@ -130,23 +128,23 @@ impl EnumeratedValue {
130128
if builder.is_default.is_some() {
131129
self.is_default = builder.is_default;
132130
}
131+
self.validate(lvl)
132+
}
133+
/// Validate the [`EnumeratedValue`].
134+
pub fn validate(&self, lvl: ValidateLevel) -> Result<(), SvdError> {
133135
if !lvl.is_disabled() {
134-
self.validate(lvl)
136+
if lvl.is_strict() {
137+
super::check_name(&self.name, "name")?;
138+
}
139+
match (self.value.is_some(), self.is_default()) {
140+
(false, false) => Err(Error::AbsentValue.into()),
141+
(true, true) if lvl.is_strict() => Err(Error::ValueAndDefault(self.value).into()),
142+
_ => Ok(()),
143+
}
135144
} else {
136145
Ok(())
137146
}
138147
}
139-
/// Validate the [`EnumeratedValue`].
140-
pub fn validate(&mut self, lvl: ValidateLevel) -> Result<(), SvdError> {
141-
if lvl.is_strict() {
142-
super::check_name(&self.name, "name")?;
143-
}
144-
match (self.value.is_some(), self.is_default()) {
145-
(false, false) => Err(Error::AbsentValue.into()),
146-
(true, true) if lvl.is_strict() => Err(Error::ValueAndDefault(self.value).into()),
147-
_ => Ok(()),
148-
}
149-
}
150148
pub(crate) fn check_range(&self, range: &core::ops::Range<u64>) -> Result<(), SvdError> {
151149
match &self.value {
152150
Some(x) if !range.contains(x) => Err(Error::OutOfRange(*x, range.clone()).into()),

svd-rs/src/enumeratedvalues.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,13 @@ impl EnumeratedValuesBuilder {
8686
}
8787
/// Validate and build a [`EnumeratedValues`].
8888
pub fn build(self, lvl: ValidateLevel) -> Result<EnumeratedValues, SvdError> {
89-
let mut evs = EnumeratedValues {
89+
let evs = EnumeratedValues {
9090
name: self.name.empty_to_none(),
9191
usage: self.usage,
9292
derived_from: self.derived_from,
9393
values: self.values.unwrap_or_default(),
9494
};
95-
if !lvl.is_disabled() {
96-
evs.validate(lvl)?;
97-
}
95+
evs.validate(lvl)?;
9896
Ok(evs)
9997
}
10098
}
@@ -135,26 +133,26 @@ impl EnumeratedValues {
135133
self.values = values;
136134
}
137135
}
138-
if !lvl.is_disabled() {
139-
self.validate(lvl)
140-
} else {
141-
Ok(())
142-
}
136+
self.validate(lvl)
143137
}
144138
/// Validate the [`EnumeratedValues`].
145-
pub fn validate(&mut self, lvl: ValidateLevel) -> Result<(), SvdError> {
146-
if lvl.is_strict() {
147-
if let Some(name) = self.name.as_ref() {
148-
super::check_name(name, "name")?;
149-
}
150-
}
151-
if let Some(_dname) = self.derived_from.as_ref() {
139+
pub fn validate(&self, lvl: ValidateLevel) -> Result<(), SvdError> {
140+
if !lvl.is_disabled() {
152141
if lvl.is_strict() {
153-
super::check_derived_name(_dname, "derivedFrom")?;
142+
if let Some(name) = self.name.as_ref() {
143+
super::check_name(name, "name")?;
144+
}
145+
}
146+
if let Some(_dname) = self.derived_from.as_ref() {
147+
if lvl.is_strict() {
148+
super::check_derived_name(_dname, "derivedFrom")?;
149+
}
150+
Ok(())
151+
} else if self.values.is_empty() {
152+
Err(Error::Empty.into())
153+
} else {
154+
Ok(())
154155
}
155-
Ok(())
156-
} else if self.values.is_empty() {
157-
Err(Error::Empty.into())
158156
} else {
159157
Ok(())
160158
}

0 commit comments

Comments
 (0)