|
impl fmt::Display for EthernetComplianceCode { |
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
|
match self.0 { |
|
0b0000_0001 => write!(f, "40G Active Cable"), |
|
0b0000_0010 => write!(f, "40GBASE-LR4"), |
|
0b0000_0100 => write!(f, "40GBASE-SR4"), |
|
0b0000_1000 => write!(f, "40GBASE-CR4"), |
|
0b0001_0000 => write!(f, "10GBASE-SR"), |
|
0b0010_0000 => write!(f, "10GBASE-LR"), |
|
0b0100_0000 => write!(f, "10GBASE-LRM"), |
|
x => write!(f, "Unknown (0x{x:02x})"), |
|
} |
|
} |
|
} |
|
|
|
impl From<EthernetComplianceCode> for String { |
|
fn from(value: EthernetComplianceCode) -> Self { |
|
format!("{value}") |
|
} |
|
} |
|
|
|
#[cfg(any(feature = "api-traits", test))] |
|
impl std::str::FromStr for EthernetComplianceCode { |
|
type Err = &'static str; |
|
|
|
fn from_str(value: &str) -> Result<Self, Self::Err> { |
|
const ERR: &str = "Unknown or malformed Ethernet compliance code"; |
|
match value { |
|
"40G Active Cable" => Ok(Self(0b0000_0001)), |
|
"40GBASE-LR4" => Ok(Self(0b0000_0010)), |
|
"40GBASE-SR4" => Ok(Self(0b0000_0100)), |
|
"40GBASE-CR4" => Ok(Self(0b0000_1000)), |
|
"10GBASE-SR" => Ok(Self(0b0001_0000)), |
|
"10GBASE-LR" => Ok(Self(0b0010_0000)), |
|
"10GBASE-LRM" => Ok(Self(0b0100_0000)), |
|
_ => Err(ERR), |
|
} |
|
} |
|
} |
We're using
DisplayandFromStrto read / write the Ethernet compliance codes when they appear in an API. Here are the impls:transceiver-control/decode/src/datapath.rs
Lines 151 to 189 in 1f94d98
These two don't match. If we get an unknown code from the module, we write it out as
Unknown (0xAA). But we have a wildcard match in theFromStrimpl that will catch that and return an error. We need to parse it the same way we emit it.