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
use crate::elementext::ElementExt;
use xmltree::Element;
use crate::types::Parse;
#[cfg(feature = "unproven")]
use crate::encode::{Encode, EncodeChildren};
#[cfg(feature = "unproven")]
use crate::new_element;
use crate::error::*;
use crate::svd::{registercluster::RegisterCluster, registerproperties::RegisterProperties};
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Clone, Debug, PartialEq)]
pub struct ClusterInfo {
pub name: String,
pub derived_from: Option<String>,
pub description: Option<String>,
pub header_struct_name: Option<String>,
pub address_offset: u32,
pub default_register_properties: RegisterProperties,
pub children: Vec<RegisterCluster>,
_extensible: (),
}
impl Parse for ClusterInfo {
type Object = ClusterInfo;
type Error = anyhow::Error;
fn parse(tree: &Element) -> Result<ClusterInfo> {
let name = tree.get_child_text("name")?;
ClusterInfo::_parse(tree, name.clone()).with_context(|| format!("In cluster `{}`", name))
}
}
impl ClusterInfo {
fn _parse(tree: &Element, name: String) -> Result<ClusterInfo> {
Ok(ClusterInfo {
name,
derived_from: tree.attributes.get("derivedFrom").map(|s| s.to_owned()),
description: tree.get_child_text_opt("description")?,
header_struct_name: tree.get_child_text_opt("headerStructName")?,
address_offset: tree.get_child_u32("addressOffset")?,
default_register_properties: RegisterProperties::parse(tree)?,
children: {
let children: Result<Vec<_>, _> = tree
.children
.iter()
.filter(|t| t.name == "register" || t.name == "cluster")
.map(RegisterCluster::parse)
.collect();
children?
},
_extensible: (),
})
}
}
#[cfg(feature = "unproven")]
impl Encode for ClusterInfo {
type Error = anyhow::Error;
fn encode(&self) -> Result<Element> {
let mut e = new_element("cluster", None);
if let Some(v) = &self.derived_from {
e.attributes
.insert(String::from("derivedFrom"), format!("{}", v));
}
e.children
.push(new_element("description", self.description.clone()));
if let Some(v) = &self.header_struct_name {
e.children
.push(new_element("headerStructName", Some(v.clone())));
}
e.children.push(new_element(
"addressOffset",
Some(format!("{}", self.address_offset)),
));
e.children
.extend(self.default_register_properties.encode()?);
for c in &self.children {
e.children.push(c.encode()?);
}
Ok(e)
}
}