Skip to content

Commit

Permalink
add Address::parse
Browse files Browse the repository at this point in the history
  • Loading branch information
thvdveld committed Oct 17, 2024
1 parent 99cd4b9 commit 55e6881
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions dot15d4-frame/src/addressing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,33 @@ impl Address {
pub fn is_extended(&self) -> bool {
matches!(self, Address::Extended(_))
}

/// Parse an address from a string.
#[cfg(any(feature = "std", test))]
pub fn parse(s: &str) -> Result<Self> {
if s.is_empty() {
return Ok(Address::Absent);
}

let parts: Vec<&str> = s.split(':').collect();
match parts.len() {
2 => {
let mut bytes = [0u8; 2];
for (i, part) in parts.iter().enumerate() {
bytes[i] = u8::from_str_radix(part, 16).unwrap();
}
Ok(Address::Short(bytes))
}
8 => {
let mut bytes = [0u8; 8];
for (i, part) in parts.iter().enumerate() {
bytes[i] = u8::from_str_radix(part, 16).unwrap();
}
Ok(Address::Extended(bytes))
}
_ => Err(Error),
}
}
}

impl From<Address> for AddressingMode {
Expand Down Expand Up @@ -620,4 +647,24 @@ mod tests {
check!((Ieee802154_2020, Absent, Absent, false) -> Some((false, Absent, false, Absent)));
check!((Ieee802154_2020, Absent, Absent, true) -> Some((true, Absent, false, Absent)));
}

#[test]
fn parse() {
let mut addresses = vec![
("", Address::Absent),
("ff:ff", Address::Short([0xff, 0xff])),
("ff:fe", Address::Short([0xff, 0xfe])),
("ff:ff:ff:ff:ff:ff:ff:ff", Address::Extended([0xff; 8])),
("01:01:01:01:01:01:01:01", Address::Extended([0x01; 8])),
("00:00:00:00:00:00:00:00", Address::Extended([0x00; 8])),
(
"00:00:00:00:00:00:00:01",
Address::Extended([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]),
),
];

for (s, expected) in addresses.drain(..) {
assert_eq!(Address::parse(s).unwrap(), expected);
}
}
}

0 comments on commit 55e6881

Please sign in to comment.