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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
use core::{fmt, str::FromStr};
use crate::{parser, MacAddr6, MacAddr8, ParseError};
#[derive(Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
pub enum MacAddr {
V6(MacAddr6),
V8(MacAddr8),
}
impl MacAddr {
pub fn is_v6(&self) -> bool {
match self {
MacAddr::V6(_) => true,
MacAddr::V8(_) => false,
}
}
pub fn is_v8(&self) -> bool {
match self {
MacAddr::V6(_) => false,
MacAddr::V8(_) => true,
}
}
pub fn as_bytes(&self) -> &[u8] {
match self {
MacAddr::V6(addr) => addr.as_bytes(),
MacAddr::V8(addr) => addr.as_bytes(),
}
}
}
impl From<MacAddr6> for MacAddr {
fn from(addr: MacAddr6) -> Self {
MacAddr::V6(addr)
}
}
impl From<MacAddr8> for MacAddr {
fn from(addr: MacAddr8) -> Self {
MacAddr::V8(addr)
}
}
impl FromStr for MacAddr {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
parser::Parser::new(s).read_addr()
}
}
impl From<[u8; 6]> for MacAddr {
fn from(bytes: [u8; 6]) -> Self {
MacAddr::V6(MacAddr6::from(bytes))
}
}
impl From<[u8; 8]> for MacAddr {
fn from(bytes: [u8; 8]) -> Self {
MacAddr::V8(MacAddr8::from(bytes))
}
}
impl fmt::Display for MacAddr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
MacAddr::V6(v6) => fmt::Display::fmt(v6, f),
MacAddr::V8(v8) => fmt::Display::fmt(v8, f),
}
}
}