impl FromStr for EUI*Addr and add some tests

This commit is contained in:
Stefan Bühler 2020-03-07 17:22:22 +01:00
parent 4b228e007f
commit d10da61a1c
2 changed files with 40 additions and 10 deletions

View File

@ -55,6 +55,16 @@ impl fmt::Debug for EUI48Addr {
}
}
impl std::str::FromStr for EUI48Addr {
type Err = failure::Error;
fn from_str(s: &str) -> Result<Self> {
let mut buf = [0u8; 6];
parse_eui_hyphens(&mut buf, s)?;
Ok(EUI48Addr(buf))
}
}
impl DnsPacketData for EUI48Addr {
fn deserialize(data: &mut Cursor<Bytes>) -> Result<Self> {
let mut buf = [0u8; 6];
@ -71,13 +81,9 @@ impl DnsPacketData for EUI48Addr {
impl DnsTextData for EUI48Addr {
fn dns_parse(_context: &DnsTextContext, data: &mut &str) -> Result<Self> {
let field = next_field(data)?;
let mut buf = [0u8; 6];
parse_eui_hyphens(&mut buf, field)?;
Ok(EUI48Addr(buf))
next_field(data)?.parse()
}
// format might fail if there is no (known) text representation.
fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result {
write!(f, "{}", self)
}
@ -110,6 +116,16 @@ impl fmt::Debug for EUI64Addr {
}
}
impl std::str::FromStr for EUI64Addr {
type Err = failure::Error;
fn from_str(s: &str) -> Result<Self> {
let mut buf = [0u8; 8];
parse_eui_hyphens(&mut buf, s)?;
Ok(EUI64Addr(buf))
}
}
impl DnsPacketData for EUI64Addr {
fn deserialize(data: &mut Cursor<Bytes>) -> Result<Self> {
let mut buf = [0u8; 8];
@ -126,13 +142,9 @@ impl DnsPacketData for EUI64Addr {
impl DnsTextData for EUI64Addr {
fn dns_parse(_context: &DnsTextContext, data: &mut &str) -> Result<Self> {
let field = next_field(data)?;
let mut buf = [0u8; 8];
parse_eui_hyphens(&mut buf, field)?;
Ok(EUI64Addr(buf))
next_field(data)?.parse()
}
// format might fail if there is no (known) text representation.
fn dns_format(&self, f: &mut DnsTextFormatter) -> fmt::Result {
write!(f, "{}", self)
}

View File

@ -775,22 +775,40 @@ fn test_SPF() {
#[test]
fn test_EUI48() {
use crate::common_types::EUI48Addr;
check(
types::EUI48,
"00-11-22-33-44-55",
None,
b"\x00\x11\x22\x33\x44\x55",
);
assert_eq!(
"00-00-5e-00-53-2a".parse::<EUI48Addr>().unwrap(),
EUI48Addr([0x00, 0x00, 0x5e, 0x00, 0x53, 0x2a]),
);
assert!("00-00-5e-00-53-".parse::<EUI48Addr>().is_err());
}
#[test]
fn test_EUI64() {
use crate::common_types::EUI64Addr;
check(
types::EUI64,
"00-11-22-33-44-55-66-77",
None,
b"\x00\x11\x22\x33\x44\x55\x66\x77",
);
assert_eq!(
"00-00-5e-ef-10-00-00-2a".parse::<EUI64Addr>().unwrap(),
EUI64Addr([0x00, 0x00, 0x5e, 0xef, 0x10, 0x00, 0x00, 0x2a]),
);
assert!("00-00-5e-ef-10-00-00-".parse::<EUI64Addr>().is_err());
}
#[test]