Skip to content

Commit f3b50a0

Browse files
bors[bot]japaric
andcommitted
Merge #61
61: encode/field: only generate a description element when present r=ryankurte a=japaric without this parsing the output of encode fails when there is no description field the first commit can be ignored as it just runs cargo-fmt Co-authored-by: Jorge Aparicio <[email protected]>
2 parents f89c08a + efe7252 commit f3b50a0

File tree

2 files changed

+67
-14
lines changed

2 files changed

+67
-14
lines changed

src/svd/field.rs

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,16 @@ impl Field {
8484
impl Encode for Field {
8585
type Error = SVDError;
8686
fn encode(&self) -> Result<Element, SVDError> {
87+
let mut children = vec![new_element("name", Some(self.name.clone()))];
88+
89+
if let Some(ref description) = self.description {
90+
children.push(new_element("description", Some(description.clone())))
91+
}
92+
8793
let mut elem = Element {
8894
name: String::from("field"),
8995
attributes: HashMap::new(),
90-
children: vec![
91-
new_element("name", Some(self.name.clone())),
92-
new_element("description", self.description.clone()),
93-
],
96+
children,
9497
text: None,
9598
};
9699

@@ -189,7 +192,53 @@ mod tests {
189192
</enumeratedValue>
190193
</enumeratedValues>
191194
</field>
192-
"
195+
",
196+
),
197+
// almost the same test but description info is missing
198+
(
199+
Field {
200+
name: String::from("MODE"),
201+
description: None,
202+
bit_range: BitRange {
203+
offset: 24,
204+
width: 2,
205+
range_type: BitRangeType::OffsetWidth,
206+
},
207+
access: Some(Access::ReadWrite),
208+
enumerated_values: vec![EnumeratedValues {
209+
name: None,
210+
usage: None,
211+
derived_from: None,
212+
values: vec![EnumeratedValue {
213+
name: String::from("WS0"),
214+
description: Some(String::from(
215+
"Zero wait-states inserted in fetch or read transfers",
216+
)),
217+
value: Some(0),
218+
is_default: None,
219+
_extensible: (),
220+
}],
221+
_extensible: (),
222+
}],
223+
write_constraint: None,
224+
modified_write_values: None,
225+
_extensible: (),
226+
},
227+
"
228+
<field>
229+
<name>MODE</name>
230+
<bitOffset>24</bitOffset>
231+
<bitWidth>2</bitWidth>
232+
<access>read-write</access>
233+
<enumeratedValues>
234+
<enumeratedValue>
235+
<name>WS0</name>
236+
<description>Zero wait-states inserted in fetch or read transfers</description>
237+
<value>0x00000000</value>
238+
</enumeratedValue>
239+
</enumeratedValues>
240+
</field>
241+
",
193242
),
194243
];
195244

src/svd/registerinfo.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,19 @@ impl Encode for RegisterInfo {
186186

187187
match self.fields {
188188
Some(ref v) => {
189-
let children: Result<Vec<Element>, SVDError> =
190-
v.iter().map(Field::encode).collect();
191-
let fields = Element {
192-
name: String::from("fields"),
193-
attributes: HashMap::new(),
194-
children: children?,
195-
text: None,
196-
};
197-
elem.children.push(fields);
189+
let children = v
190+
.iter()
191+
.map(Field::encode)
192+
.collect::<Result<Vec<Element>, SVDError>>()?;
193+
if !children.is_empty() {
194+
let fields = Element {
195+
name: String::from("fields"),
196+
attributes: HashMap::new(),
197+
children,
198+
text: None,
199+
};
200+
elem.children.push(fields);
201+
}
198202
}
199203
None => (),
200204
};

0 commit comments

Comments
 (0)