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
// SPDX-License-Identifier: MIT

const ETH_P_8021Q: u16 = 0x8100;
const ETH_P_8021AD: u16 = 0x88A8;

#[derive(Debug, PartialEq, Eq, Clone, Copy, Default)]
#[non_exhaustive]
#[repr(u16)]
// VLAN protocol seldom add new, so no Other for this enum.
pub enum VlanProtocol {
    #[default]
    Ieee8021Q = ETH_P_8021Q,
    Ieee8021Ad = ETH_P_8021AD,
}

impl From<u16> for VlanProtocol {
    fn from(d: u16) -> Self {
        match d {
            ETH_P_8021Q => Self::Ieee8021Q,
            ETH_P_8021AD => Self::Ieee8021Ad,
            _ => {
                log::warn!(
                    "BUG: Got unknown VLAN protocol {}, treating as {}",
                    d,
                    Self::Ieee8021Q
                );
                Self::Ieee8021Q
            }
        }
    }
}

impl From<VlanProtocol> for u16 {
    fn from(v: VlanProtocol) -> u16 {
        match v {
            VlanProtocol::Ieee8021Q => ETH_P_8021Q,
            VlanProtocol::Ieee8021Ad => ETH_P_8021AD,
        }
    }
}

impl std::fmt::Display for VlanProtocol {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                VlanProtocol::Ieee8021Q => "802.1q",
                VlanProtocol::Ieee8021Ad => "802.1ad",
            }
        )
    }
}