Skip to content

Commit afd072d

Browse files
committed
bond: add FromStr/Display for BondLacpRate
Add FromStr and Display trait implementations for BondLacpRate using "slow"/"fast" strings. Unit test included. Signed-off-by: Gris Ge <cnfourt@gmail.com>
1 parent 35d39ab commit afd072d

2 files changed

Lines changed: 34 additions & 0 deletions

File tree

src/link/link_info/bond.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,32 @@ impl From<BondLacpRate> for u8 {
720720
}
721721
}
722722

723+
impl std::fmt::Display for BondLacpRate {
724+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
725+
match self {
726+
BondLacpRate::Slow => f.write_str("slow"),
727+
BondLacpRate::Fast => f.write_str("fast"),
728+
BondLacpRate::Other(v) => write!(f, "{v}"),
729+
}
730+
}
731+
}
732+
733+
impl std::str::FromStr for BondLacpRate {
734+
type Err = DecodeError;
735+
736+
fn from_str(s: &str) -> Result<Self, Self::Err> {
737+
Ok(match s {
738+
"slow" => Self::Slow,
739+
"fast" => Self::Fast,
740+
_ => {
741+
return Err(DecodeError::from(format!(
742+
"unknown bond lacp rate: {s}"
743+
)))
744+
}
745+
})
746+
}
747+
}
748+
723749
const BOND_AD_STABLE: u8 = 0;
724750
const BOND_AD_BANDWIDTH: u8 = 1;
725751
const BOND_AD_COUNT: u8 = 2;

src/link/tests/bond.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,3 +316,11 @@ fn test_bond_ad_select_from_str() {
316316
);
317317
assert!(BondAdSelect::from_str("bogus").is_err());
318318
}
319+
320+
#[test]
321+
fn test_bond_lacp_rate_from_str() {
322+
use std::str::FromStr;
323+
assert_eq!(BondLacpRate::Slow, "slow".parse().unwrap());
324+
assert_eq!(BondLacpRate::Fast, "fast".parse().unwrap());
325+
assert!(BondLacpRate::from_str("bogus").is_err());
326+
}

0 commit comments

Comments
 (0)