Browse Source

Also parse Ethernet addresses separated by dashes (-).

This is handy because we print them separated by dashes.
whitequark 8 years ago
parent
commit
ebee5b7b58
1 changed files with 16 additions and 2 deletions
  1. 16 2
      src/parsing.rs

+ 16 - 2
src/parsing.rs

@@ -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(()));