Skip to content

Ethernet compliance codes can be emitted as strings that we can't parse #475

Description

@bnaecker

We're using Display and FromStr to read / write the Ethernet compliance codes when they appear in an API. Here are the impls:

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),
}
}
}

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 the FromStr impl that will catch that and return an error. We need to parse it the same way we emit it.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions