svd_rs/
registercluster.rs1use super::{Cluster, Register};
2
3#[cfg_attr(
5 feature = "serde",
6 derive(serde::Deserialize, serde::Serialize),
7 serde(rename_all = "lowercase")
8)]
9#[derive(Clone, Debug, PartialEq)]
10#[allow(clippy::large_enum_variant)]
11pub enum RegisterCluster {
12 Register(Register),
14 Cluster(Cluster),
16}
17
18impl From<Register> for RegisterCluster {
19 fn from(reg: Register) -> Self {
20 Self::Register(reg)
21 }
22}
23
24impl From<Cluster> for RegisterCluster {
25 fn from(cluser: Cluster) -> Self {
26 Self::Cluster(cluser)
27 }
28}
29
30impl RegisterCluster {
31 pub fn name(&self) -> &String {
33 match self {
34 Self::Register(r) => &r.name,
35 Self::Cluster(c) => &c.name,
36 }
37 }
38 pub fn description(&self) -> &Option<String> {
40 match self {
41 Self::Register(r) => &r.description,
42 Self::Cluster(c) => &c.description,
43 }
44 }
45 pub fn derived_from(&self) -> &Option<String> {
47 match self {
48 Self::Register(r) => &r.derived_from,
49 Self::Cluster(c) => &c.derived_from,
50 }
51 }
52 pub fn address_offset(&self) -> u32 {
54 match self {
55 Self::Register(r) => r.address_offset,
56 Self::Cluster(c) => c.address_offset,
57 }
58 }
59}
60
61pub struct RegisterIter<'a> {
63 pub(crate) all: std::slice::Iter<'a, RegisterCluster>,
64}
65
66impl<'a> std::iter::Iterator for RegisterIter<'a> {
67 type Item = &'a Register;
68 fn next(&mut self) -> Option<Self::Item> {
69 match self.all.next() {
70 None => None,
71 Some(RegisterCluster::Register(reg)) => Some(reg),
72 _ => self.next(),
73 }
74 }
75}
76
77pub struct RegisterIterMut<'a> {
79 pub(crate) all: std::slice::IterMut<'a, RegisterCluster>,
80}
81
82impl<'a> std::iter::Iterator for RegisterIterMut<'a> {
83 type Item = &'a mut Register;
84 fn next(&mut self) -> Option<Self::Item> {
85 match self.all.next() {
86 None => None,
87 Some(RegisterCluster::Register(reg)) => Some(reg),
88 _ => self.next(),
89 }
90 }
91}
92
93pub struct ClusterIter<'a> {
95 pub(crate) all: std::slice::Iter<'a, RegisterCluster>,
96}
97
98impl<'a> std::iter::Iterator for ClusterIter<'a> {
99 type Item = &'a Cluster;
100 fn next(&mut self) -> Option<Self::Item> {
101 match self.all.next() {
102 None => None,
103 Some(RegisterCluster::Cluster(c)) => Some(c),
104 _ => self.next(),
105 }
106 }
107}
108
109pub struct ClusterIterMut<'a> {
111 pub(crate) all: std::slice::IterMut<'a, RegisterCluster>,
112}
113
114impl<'a> std::iter::Iterator for ClusterIterMut<'a> {
115 type Item = &'a mut Cluster;
116 fn next(&mut self) -> Option<Self::Item> {
117 match self.all.next() {
118 None => None,
119 Some(RegisterCluster::Cluster(c)) => Some(c),
120 _ => self.next(),
121 }
122 }
123}
124
125pub struct AllRegistersIter<'a> {
127 pub(crate) rem: Vec<&'a RegisterCluster>,
128}
129
130impl<'a> std::iter::Iterator for AllRegistersIter<'a> {
131 type Item = &'a Register;
132 fn next(&mut self) -> Option<Self::Item> {
133 while let Some(b) = self.rem.pop() {
134 match b {
135 RegisterCluster::Register(reg) => {
136 return Some(reg);
137 }
138 RegisterCluster::Cluster(cluster) => {
139 for c in cluster.children.iter().rev() {
140 self.rem.push(c);
141 }
142 }
143 }
144 }
145 None
146 }
147}
148
149pub struct AllRegistersIterMut<'a> {
151 pub(crate) rem: Vec<&'a mut RegisterCluster>,
152}
153
154impl<'a> std::iter::Iterator for AllRegistersIterMut<'a> {
155 type Item = &'a mut Register;
156 fn next(&mut self) -> Option<Self::Item> {
157 while let Some(b) = self.rem.pop() {
158 match b {
159 RegisterCluster::Register(reg) => {
160 return Some(reg);
161 }
162 RegisterCluster::Cluster(cluster) => {
163 for c in cluster.children.iter_mut().rev() {
164 self.rem.push(c);
165 }
166 }
167 }
168 }
169 None
170 }
171}