Skip to content

Commit cebb4a8

Browse files
committed
encode/field: only generate a description element when present
1 parent 1888f10 commit cebb4a8

File tree

1 file changed

+54
-5
lines changed

1 file changed

+54
-5
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

0 commit comments

Comments
 (0)