svd_encoder/
dimelement.rs

1use crate::config::{change_case, format_number};
2
3use super::{new_node, Config, Element, Encode, EncodeError};
4
5impl Encode for crate::svd::DimElement {
6    type Error = EncodeError;
7
8    fn encode_with_config(&self, config: &Config) -> Result<Element, EncodeError> {
9        let mut e = Element::new("dimElement");
10
11        e.children
12            .push(new_node("dim", format_number(self.dim, config.dim_dim)));
13        e.children.push(new_node(
14            "dimIncrement",
15            format_number(self.dim_increment, config.dim_increment),
16        ));
17
18        if let Some(di) = &self.dim_index {
19            e.children
20                .push(if let Some(range) = self.indexes_as_range() {
21                    new_node("dimIndex", format!("{}-{}", range.start(), range.end()))
22                } else {
23                    new_node("dimIndex", di.join(","))
24                });
25        }
26
27        if let Some(dim_name) = &self.dim_name {
28            e.children.push(new_node("dimName", dim_name.clone()))
29        }
30
31        if let Some(v) = &self.dim_array_index {
32            e.children.push(v.encode_node_with_config(config)?);
33        }
34
35        Ok(e)
36    }
37}
38
39impl Encode for crate::svd::DimArrayIndex {
40    type Error = EncodeError;
41
42    fn encode_with_config(&self, config: &Config) -> Result<Element, EncodeError> {
43        let mut base = Element::new("dimArrayIndex");
44
45        if let Some(d) = &self.header_enum_name {
46            base.children.push(new_node(
47                "headerEnumName",
48                change_case(d, config.dim_array_index_header_enum_name),
49            ));
50        }
51
52        for v in &self.values {
53            base.children.push(v.encode_node_with_config(config)?);
54        }
55
56        Ok(base)
57    }
58}