net_util: Implement FromStr for MacAddr

This allows it to be used with str::parse().

Signed-off-by: Rob Bradford <robert.bradford@intel.com>
This commit is contained in:
Rob Bradford 2020-03-31 16:43:09 +01:00
parent 8754720e2d
commit a5747a843e

View File

@ -9,6 +9,7 @@ use rand::Rng;
use std::fmt;
use std::io;
use std::result::Result;
use std::str::FromStr;
use serde::de::{Deserialize, Deserializer, Error};
use serde::ser::{Serialize, Serializer};
@ -120,6 +121,18 @@ impl<'de> Deserialize<'de> for MacAddr {
}
}
pub enum MacAddrParseError {
InvalidValue(String),
}
impl FromStr for MacAddr {
type Err = MacAddrParseError;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
MacAddr::parse_str(s).map_err(|_| MacAddrParseError::InvalidValue(s.to_owned()))
}
}
#[cfg(test)]
mod tests {
extern crate serde_json;