Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/link_mode/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const DUPLEX_HALF: u8 = 0x00;
const DUPLEX_FULL: u8 = 0x01;
const DUPLEX_UNKNOWN: u8 = 0xff;

#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum EthtoolLinkModeDuplex {
Half,
Full,
Expand All @@ -42,6 +42,17 @@ impl From<u8> for EthtoolLinkModeDuplex {
}
}

impl From<EthtoolLinkModeDuplex> for u8 {
fn from(v: EthtoolLinkModeDuplex) -> u8 {
match v {
EthtoolLinkModeDuplex::Half => DUPLEX_HALF,
EthtoolLinkModeDuplex::Full => DUPLEX_FULL,
EthtoolLinkModeDuplex::Unknown => DUPLEX_UNKNOWN,
EthtoolLinkModeDuplex::Other(d) => d,
}
}
}
Comment on lines +45 to +54

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This new From implementation is a great addition. To ensure its correctness and prevent future regressions, it would be beneficial to add unit tests for the conversions between EthtoolLinkModeDuplex and u8.

You could add a test module at the end of this file, like so:

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_duplex_conversion() {
        assert_eq!(u8::from(EthtoolLinkModeDuplex::Half), DUPLEX_HALF);
        assert_eq!(u8::from(EthtoolLinkModeDuplex::Full), DUPLEX_FULL);
        assert_eq!(u8::from(EthtoolLinkModeDuplex::Unknown), DUPLEX_UNKNOWN);
        assert_eq!(u8::from(EthtoolLinkModeDuplex::Other(42)), 42);

        assert_eq!(EthtoolLinkModeDuplex::from(DUPLEX_HALF), EthtoolLinkModeDuplex::Half);
        assert_eq!(EthtoolLinkModeDuplex::from(DUPLEX_FULL), EthtoolLinkModeDuplex::Full);
        assert_eq!(EthtoolLinkModeDuplex::from(DUPLEX_UNKNOWN), EthtoolLinkModeDuplex::Unknown);
        assert_eq!(EthtoolLinkModeDuplex::from(42), EthtoolLinkModeDuplex::Other(42));
    }
}


#[derive(Debug, PartialEq, Eq, Clone)]
pub enum EthtoolLinkModeAttr {
Header(Vec<EthtoolHeader>),
Expand Down