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
/// This module provides a type for a MAC address, represented as a 6-byte array of unsigned
/// integers. It implements `Display` and `Debug` traits for displaying a MAC address in the
/// standard colon-separated format.
///
/// # Example
///
/// ```
/// use ndisapi_rs::MacAddress;
///
/// let mac = MacAddress::from_slice(&[0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC]).unwrap();
/// assert_eq!(format!("{}", mac), "12:34:56:78:9A:BC");
/// assert_eq!(format!("{:?}", mac), "12:34:56:78:9A:BC");
/// ```
use std::fmt::{Debug, Display, Formatter, Result};

const ETHER_ADDR_LENGTH: usize = 6;

/// A MAC address represented as a 6-byte array of unsigned integers.
#[derive(Default, PartialEq, Eq, Clone, Copy, Hash, PartialOrd, Ord)]
pub struct MacAddress([u8; ETHER_ADDR_LENGTH]);

impl Display for MacAddress {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        write!(
            f,
            "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
            self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5]
        )
    }
}

impl Debug for MacAddress {
    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
        write!(
            f,
            "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
            self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5]
        )
    }
}

impl MacAddress {
    /// Creates a new `MacAddress` instance from a slice of bytes.
    ///
    /// # Arguments
    ///
    /// * `slice` - A slice of bytes representing a MAC address.
    ///
    /// # Returns
    ///
    /// An `Option` containing a `MacAddress` instance if the slice has a length of `ETHER_ADDR_LENGTH`
    /// bytes, `None` otherwise.
    pub fn from_slice(slice: &[u8]) -> Option<MacAddress> {
        let mut mac_address = MacAddress::default();
        if slice.len() < ETHER_ADDR_LENGTH {
            None
        } else {
            mac_address.0[..ETHER_ADDR_LENGTH].copy_from_slice(&slice[..ETHER_ADDR_LENGTH]);
            Some(mac_address)
        }
    }

    /// Returns a reference to the internal byte array of the `MacAddress` instance.
    pub fn get(&self) -> &[u8; 6] {
        &self.0
    }

    /// Returns a mutable reference to the internal byte array of the `MacAddress` instance.
    pub fn get_mut(&mut self) -> &mut [u8; 6] {
        &mut self.0
    }
}