Skip to content

Commit

Permalink
compare path with string
Browse files Browse the repository at this point in the history
  • Loading branch information
burrbull committed Feb 26, 2024
1 parent 58c1a48 commit 7958f20
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions svd-parser/src/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,28 @@ impl BlockPath {
}
}

impl PartialEq<str> for BlockPath {
fn eq(&self, other: &str) -> bool {
if other.split('.').count() != self.path.len() + 1 {
return false;
}
let mut parts = other.split('.');
if let Some(part1) = parts.next() {
if self.peripheral != part1 {
return false;
}
for p in parts.zip(self.path.iter()) {
if p.0 != p.1 {
return false;
}
}
true
} else {
false
}
}
}

impl fmt::Display for BlockPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.peripheral)?;
Expand Down Expand Up @@ -95,6 +117,16 @@ impl RegisterPath {
}
}

impl PartialEq<str> for RegisterPath {
fn eq(&self, other: &str) -> bool {
if let Some((block, reg)) = other.rsplit_once('.') {
self.name == reg && &self.block == block
} else {
false
}
}
}

impl fmt::Display for RegisterPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.block.fmt(f)?;
Expand Down Expand Up @@ -145,6 +177,16 @@ impl FieldPath {
}
}

impl PartialEq<str> for FieldPath {
fn eq(&self, other: &str) -> bool {
if let Some((reg, field)) = other.rsplit_once('.') {
self.name == field && &self.register == reg
} else {
false
}
}
}

impl fmt::Display for FieldPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.register.fmt(f)?;
Expand Down Expand Up @@ -179,6 +221,16 @@ impl EnumPath {
}
}

impl PartialEq<str> for EnumPath {
fn eq(&self, other: &str) -> bool {
if let Some((field, evs)) = other.rsplit_once('.') {
self.name == evs && &self.field == field
} else {
false
}
}
}

impl fmt::Display for EnumPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.field.fmt(f)?;
Expand Down

0 comments on commit 7958f20

Please sign in to comment.