Skip to content

Commit 992f0bb

Browse files
authored
Merge pull request #260 from rust-embedded/path-eq
compare path with string
2 parents 58c1a48 + 7958f20 commit 992f0bb

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

svd-parser/src/expand.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,28 @@ impl BlockPath {
5656
}
5757
}
5858

59+
impl PartialEq<str> for BlockPath {
60+
fn eq(&self, other: &str) -> bool {
61+
if other.split('.').count() != self.path.len() + 1 {
62+
return false;
63+
}
64+
let mut parts = other.split('.');
65+
if let Some(part1) = parts.next() {
66+
if self.peripheral != part1 {
67+
return false;
68+
}
69+
for p in parts.zip(self.path.iter()) {
70+
if p.0 != p.1 {
71+
return false;
72+
}
73+
}
74+
true
75+
} else {
76+
false
77+
}
78+
}
79+
}
80+
5981
impl fmt::Display for BlockPath {
6082
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6183
f.write_str(&self.peripheral)?;
@@ -95,6 +117,16 @@ impl RegisterPath {
95117
}
96118
}
97119

120+
impl PartialEq<str> for RegisterPath {
121+
fn eq(&self, other: &str) -> bool {
122+
if let Some((block, reg)) = other.rsplit_once('.') {
123+
self.name == reg && &self.block == block
124+
} else {
125+
false
126+
}
127+
}
128+
}
129+
98130
impl fmt::Display for RegisterPath {
99131
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100132
self.block.fmt(f)?;
@@ -145,6 +177,16 @@ impl FieldPath {
145177
}
146178
}
147179

180+
impl PartialEq<str> for FieldPath {
181+
fn eq(&self, other: &str) -> bool {
182+
if let Some((reg, field)) = other.rsplit_once('.') {
183+
self.name == field && &self.register == reg
184+
} else {
185+
false
186+
}
187+
}
188+
}
189+
148190
impl fmt::Display for FieldPath {
149191
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150192
self.register.fmt(f)?;
@@ -179,6 +221,16 @@ impl EnumPath {
179221
}
180222
}
181223

224+
impl PartialEq<str> for EnumPath {
225+
fn eq(&self, other: &str) -> bool {
226+
if let Some((field, evs)) = other.rsplit_once('.') {
227+
self.name == evs && &self.field == field
228+
} else {
229+
false
230+
}
231+
}
232+
}
233+
182234
impl fmt::Display for EnumPath {
183235
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
184236
self.field.fmt(f)?;

0 commit comments

Comments
 (0)