1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! MAC address (de)serialisation.

use std::{fmt, str::FromStr};

use macaddr::{MacAddr6, ParseError};
use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer};

/// A MAC address.
///
/// This type’s entire purpose is to serialize and deserialize from the string
/// representation of a MAC address, rather than `[u8; 6]` as the underlying
/// type does.
#[derive(Debug, Default, Hash, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
pub struct MacAddr(pub MacAddr6);

impl From<MacAddr6> for MacAddr {
	fn from(m: MacAddr6) -> Self {
		Self(m)
	}
}

impl From<MacAddr> for MacAddr6 {
	fn from(m: MacAddr) -> Self {
		m.0
	}
}

impl fmt::Display for MacAddr {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		self.0.fmt(f)
	}
}

impl FromStr for MacAddr {
	type Err = ParseError;

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		MacAddr6::from_str(s).map(Self)
	}
}

impl Serialize for MacAddr {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: Serializer,
	{
		self.to_string().serialize(serializer)
	}
}

impl<'de> Deserialize<'de> for MacAddr {
	fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
	where
		D: Deserializer<'de>,
	{
		let j = String::deserialize(deserializer)?;
		Self::from_str(&j).map_err(Error::custom)
	}
}