net_parser_rs/
common.rs

1use std;
2
3pub const MAC_LENGTH: usize = 6;
4
5#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
6pub struct MacAddress(pub [u8; MAC_LENGTH]);
7
8pub type Vlan = u16;
9
10pub type Port = u16;
11
12//impl Default for MacAddress {
13//    fn default() -> Self {
14//        MacAddress([0u8; MAC_LENGTH])
15//    }
16//}
17
18impl std::fmt::Display for MacAddress {
19    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
20        write!(
21            f,
22            "{:02x}:{:02x}:{:02x}:{:02x}:{:02x}:{:02x}",
23            self.0[0], self.0[1], self.0[2], self.0[3], self.0[4], self.0[5],
24        )
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn format_mac_address() {
34        let mac = MacAddress([0u8, 1u8, 2u8, 3u8, 4u8, 5u8]);
35
36        assert_eq!(format!("{}", mac), "00:01:02:03:04:05".to_string());
37    }
38}