Skip to content

Commit

Permalink
Also parse Ethernet addresses separated by dashes (-).
Browse files Browse the repository at this point in the history
This is handy because we print them separated by dashes.
whitequark committed Jan 26, 2017
1 parent e436356 commit ebee5b7
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/parsing.rs
Original file line number Diff line number Diff line change
@@ -95,17 +95,27 @@ impl<'a> Parser<'a> {
}
}

fn accept_mac(&mut self) -> Result<EthernetAddress> {
fn accept_mac_joined_with(&mut self, separator: u8) -> Result<EthernetAddress> {
let mut octets = [0u8; 6];
for n in 0..6 {
octets[n] = self.accept_number(2, 0x100, true)? as u8;
if n != 5 {
self.accept_char(b':')?;
self.accept_char(separator)?;
}
}
Ok(EthernetAddress(octets))
}

fn accept_mac(&mut self) -> Result<EthernetAddress> {
if let Some(mac) = self.try(|p| p.accept_mac_joined_with(b'-')) {
return Ok(mac)
}
if let Some(mac) = self.try(|p| p.accept_mac_joined_with(b':')) {
return Ok(mac)
}
Err(())
}

fn accept_ipv4(&mut self) -> Result<Ipv4Address> {
let mut octets = [0u8; 4];
for n in 0..4 {
@@ -164,6 +174,10 @@ mod test {
Ok(EthernetAddress([0xcd, 0xef, 0x10, 0x00, 0x00, 0x00])));
assert_eq!(EthernetAddress::parse("00:00:00:ab:cd:ef"),
Ok(EthernetAddress([0x00, 0x00, 0x00, 0xab, 0xcd, 0xef])));
assert_eq!(EthernetAddress::parse("00-00-00-ab-cd-ef"),
Ok(EthernetAddress([0x00, 0x00, 0x00, 0xab, 0xcd, 0xef])));
assert_eq!(EthernetAddress::parse("AB-CD-EF-00-00-00"),
Ok(EthernetAddress([0xab, 0xcd, 0xef, 0x00, 0x00, 0x00])));
assert_eq!(EthernetAddress::parse("100:00:00:00:00:00"), Err(()));
assert_eq!(EthernetAddress::parse("002:00:00:00:00:00"), Err(()));
assert_eq!(EthernetAddress::parse("02:00:00:00:00:000"), Err(()));

0 comments on commit ebee5b7

Please sign in to comment.