#![forbid(unsafe_code)]
use core::fmt;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
#[repr(transparent)]
pub struct EtherType(pub u16);
macro_rules! names {
($($(#[$doc:meta])* $name:ident = $value:literal;)*) => {
impl EtherType {
$($(#[$doc])* pub const $name: EtherType = EtherType($value);)*
pub const fn name(self) -> Option<&'static str> {
match self.0 {
$($value => Some(stringify!($name)),)*
_ => None,
}
}
}
#[cfg(test)]
const NAMED: &[(EtherType, &str)] = &[$((EtherType::$name, stringify!($name)),)*];
};
}
names! {
IPV4 = 0x0800;
ARP = 0x0806;
AVTP = 0x22F0;
VLAN = 0x8100;
IPV6 = 0x86DD;
EAPOL = 0x888E;
PROFINET = 0x8892;
ETHERCAT = 0x88A4;
QINQ = 0x88A8;
LOCAL_EXPERIMENTAL_1 = 0x88B5;
LOCAL_EXPERIMENTAL_2 = 0x88B6;
GOOSE = 0x88B8;
GSE = 0x88B9;
SAMPLED_VALUES = 0x88BA;
LLDP = 0x88CC;
MRP = 0x88E3;
MACSEC = 0x88E5;
PTP = 0x88F7;
PRP = 0x88FB;
CFM = 0x8902;
HSR = 0x892F;
}
impl EtherType {
pub const fn value(self) -> u16 {
self.0
}
pub const fn is_length(self) -> bool {
self.0 < 0x0600
}
}
impl From<u16> for EtherType {
fn from(v: u16) -> EtherType {
EtherType(v)
}
}
impl From<&u16> for EtherType {
fn from(v: &u16) -> EtherType {
EtherType(*v)
}
}
impl From<&EtherType> for EtherType {
fn from(t: &EtherType) -> EtherType {
*t
}
}
impl From<EtherType> for u16 {
fn from(t: EtherType) -> u16 {
t.0
}
}
impl fmt::Display for EtherType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.name() {
Some(name) => write!(f, "{name} ({:#06x})", self.0),
None => write!(f, "{:#06x}", self.0),
}
}
}
impl fmt::LowerHex for EtherType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::LowerHex::fmt(&self.0, f)
}
}
impl fmt::UpperHex for EtherType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::UpperHex::fmt(&self.0, f)
}
}
impl fmt::Debug for EtherType {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::TagKind;
#[test]
fn every_name_has_its_registry_value() {
let registry: &[(&str, u16)] = &[
("IPV4", 0x0800),
("ARP", 0x0806),
("AVTP", 0x22F0),
("VLAN", 0x8100),
("IPV6", 0x86DD),
("EAPOL", 0x888E),
("PROFINET", 0x8892),
("ETHERCAT", 0x88A4),
("QINQ", 0x88A8),
("LOCAL_EXPERIMENTAL_1", 0x88B5),
("LOCAL_EXPERIMENTAL_2", 0x88B6),
("GOOSE", 0x88B8),
("GSE", 0x88B9),
("SAMPLED_VALUES", 0x88BA),
("LLDP", 0x88CC),
("MRP", 0x88E3),
("MACSEC", 0x88E5),
("PTP", 0x88F7),
("PRP", 0x88FB),
("CFM", 0x8902),
("HSR", 0x892F),
];
assert_eq!(NAMED.len(), registry.len());
for ((t, name), (rname, rvalue)) in NAMED.iter().zip(registry) {
assert_eq!((*name, t.0), (*rname, *rvalue));
assert_eq!(t.name(), Some(*name));
assert!(!t.is_length());
}
let mut values: Vec<u16> = NAMED.iter().map(|(t, _)| t.0).collect();
values.sort_unstable();
values.dedup();
assert_eq!(values.len(), NAMED.len(), "no two names share a value");
assert_eq!(EtherType::VLAN.0, TagKind::Customer.tpid());
assert_eq!(EtherType::QINQ.0, TagKind::Service.tpid());
}
#[test]
fn lengths_and_unnamed_values_print_as_numbers() {
for v in [0x0000, 0x0050, 0x05ff] {
assert!(EtherType(v).is_length());
assert_eq!(EtherType(v).name(), None);
}
assert!(!EtherType(0x0600).is_length());
assert_eq!(format!("{}", EtherType(0x0050)), "0x0050");
assert_eq!(format!("{:?}", EtherType::PTP), "PTP (0x88f7)");
assert_eq!(format!("{}", EtherType(0x9100)), "0x9100", "pre-standard Q-in-Q is not named");
assert_eq!(u16::from(EtherType::from(0x88cc)), 0x88cc);
assert_eq!(format!("{:04x} {:04X}", EtherType::LLDP, EtherType::LLDP), "88cc 88CC");
}
}