• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use core::{fmt, str::FromStr};
2 
3 use crate::{parser, MacAddr6, MacAddr8, ParseError};
4 
5 /// A MAC address, either in *EUI-48* or *EUI-64* format.
6 #[derive(Debug, Hash, Eq, PartialEq, Ord, PartialOrd, Copy, Clone)]
7 pub enum MacAddr {
8     V6(MacAddr6),
9     V8(MacAddr8),
10 }
11 
12 impl MacAddr {
13     /// Returns `true` if the address is `MacAddr6` address.
14     ///
15     /// ## Example
16     ///
17     /// ```rust
18     /// # use macaddr::{MacAddr, MacAddr6};
19     /// let addr = MacAddr::from([0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67]);
20     ///
21     /// assert_eq!(addr.is_v6(), true);
22     /// assert_eq!(addr.is_v8(), false);
23     /// ```
is_v6(&self) -> bool24     pub fn is_v6(&self) -> bool {
25         match self {
26             MacAddr::V6(_) => true,
27             MacAddr::V8(_) => false,
28         }
29     }
30 
31     /// Returns `true` if the address is `MacAddr8` address.
32     ///
33     /// ## Example
34     ///
35     /// ```rust
36     /// # use macaddr::{MacAddr, MacAddr8};
37     /// let addr = MacAddr::from([0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67, 0x89, 0xAB]);
38     ///
39     /// assert_eq!(addr.is_v6(), false);
40     /// assert_eq!(addr.is_v8(), true);
41     /// ```
is_v8(&self) -> bool42     pub fn is_v8(&self) -> bool {
43         match self {
44             MacAddr::V6(_) => false,
45             MacAddr::V8(_) => true,
46         }
47     }
48 
49     /// Converts a `MacAddr` address to a byte slice.
50     ///
51     /// Length of the returned slice is depends on the enum member used.
52     ///
53     /// ## Example
54     ///
55     /// ```rust
56     /// # use macaddr::{MacAddr, MacAddr6};
57     /// let addr = MacAddr::from([0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67]);
58     ///
59     /// assert_eq!(addr.as_bytes(), &[0xAC, 0xDE, 0x48, 0x23, 0x45, 0x67]);
60     /// ```
as_bytes(&self) -> &[u8]61     pub fn as_bytes(&self) -> &[u8] {
62         match self {
63             MacAddr::V6(addr) => addr.as_bytes(),
64             MacAddr::V8(addr) => addr.as_bytes(),
65         }
66     }
67 }
68 
69 impl From<MacAddr6> for MacAddr {
from(addr: MacAddr6) -> Self70     fn from(addr: MacAddr6) -> Self {
71         MacAddr::V6(addr)
72     }
73 }
74 
75 impl From<MacAddr8> for MacAddr {
from(addr: MacAddr8) -> Self76     fn from(addr: MacAddr8) -> Self {
77         MacAddr::V8(addr)
78     }
79 }
80 
81 impl FromStr for MacAddr {
82     type Err = ParseError;
83 
from_str(s: &str) -> Result<Self, Self::Err>84     fn from_str(s: &str) -> Result<Self, Self::Err> {
85         parser::Parser::new(s).read_addr()
86     }
87 }
88 
89 impl From<[u8; 6]> for MacAddr {
from(bytes: [u8; 6]) -> Self90     fn from(bytes: [u8; 6]) -> Self {
91         MacAddr::V6(MacAddr6::from(bytes))
92     }
93 }
94 
95 impl From<[u8; 8]> for MacAddr {
from(bytes: [u8; 8]) -> Self96     fn from(bytes: [u8; 8]) -> Self {
97         MacAddr::V8(MacAddr8::from(bytes))
98     }
99 }
100 
101 impl fmt::Display for MacAddr {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result102     fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
103         match self {
104             MacAddr::V6(v6) => fmt::Display::fmt(v6, f),
105             MacAddr::V8(v8) => fmt::Display::fmt(v8, f),
106         }
107     }
108 }
109