-
Notifications
You must be signed in to change notification settings - Fork 75
link: bond: implement FromStr for bond enums #263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -224,6 +224,27 @@ impl std::fmt::Display for BondMode { | |
| } | ||
| } | ||
|
|
||
| impl std::str::FromStr for BondMode { | ||
| type Err = DecodeError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| Ok(match s { | ||
| "balance-rr" => Self::BalanceRr, | ||
| "active-backup" => Self::ActiveBackup, | ||
| "balance-xor" => Self::BalanceXor, | ||
| "broadcast" => Self::Broadcast, | ||
| "802.3ad" => Self::Ieee8023Ad, | ||
| "balance-tlb" => Self::BalanceTlb, | ||
| "balance-alb" => Self::BalanceAlb, | ||
| _ => { | ||
| return Err(DecodeError::from(format!( | ||
| "unknown bond mode: {s}" | ||
| ))) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] | ||
| #[non_exhaustive] | ||
| pub enum BondArpValidate { | ||
|
|
@@ -286,6 +307,27 @@ impl std::fmt::Display for BondArpValidate { | |
| } | ||
| } | ||
|
|
||
| impl std::str::FromStr for BondArpValidate { | ||
| type Err = DecodeError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| Ok(match s { | ||
| "none" => Self::None, | ||
| "active" => Self::Active, | ||
| "backup" => Self::Backup, | ||
| "all" => Self::All, | ||
| "filter" => Self::Filter, | ||
| "filter_active" => Self::FilterActive, | ||
| "filter_backup" => Self::FilterBackup, | ||
| _ => { | ||
| return Err(DecodeError::from(format!( | ||
| "unknown bond arp validate: {s}" | ||
| ))) | ||
| } | ||
| }) | ||
| } | ||
|
Comment on lines
+313
to
+328
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"none" => Ok(Self::None),
"active" => Ok(Self::Active),
"backup" => Ok(Self::Backup),
"all" => Ok(Self::All),
"filter" => Ok(Self::Filter),
"filter_active" => Ok(Self::FilterActive),
"filter_backup" => Ok(Self::FilterBackup),
_ => Err(DecodeError::from(format!("unknown bond arp validate: {s}"))),
}
} |
||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] | ||
| #[non_exhaustive] | ||
| pub enum BondPrimaryReselect { | ||
|
|
@@ -332,6 +374,23 @@ impl std::fmt::Display for BondPrimaryReselect { | |
| } | ||
| } | ||
|
|
||
| impl std::str::FromStr for BondPrimaryReselect { | ||
| type Err = DecodeError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| Ok(match s { | ||
| "always" => Self::Always, | ||
| "better" => Self::Better, | ||
| "failure" => Self::Failure, | ||
| _ => { | ||
| return Err(DecodeError::from(format!( | ||
| "unknown bond primary reselect: {s}" | ||
| ))) | ||
| } | ||
| }) | ||
| } | ||
|
Comment on lines
+380
to
+391
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"always" => Ok(Self::Always),
"better" => Ok(Self::Better),
"failure" => Ok(Self::Failure),
_ => Err(DecodeError::from(format!("unknown bond primary reselect: {s}"))),
}
} |
||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] | ||
| #[non_exhaustive] | ||
| pub enum BondXmitHashPolicy { | ||
|
|
@@ -390,6 +449,26 @@ impl std::fmt::Display for BondXmitHashPolicy { | |
| } | ||
| } | ||
|
|
||
| impl std::str::FromStr for BondXmitHashPolicy { | ||
| type Err = DecodeError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| Ok(match s { | ||
| "layer2" => Self::Layer2, | ||
| "layer34" => Self::Layer34, | ||
| "layer23" => Self::Layer23, | ||
| "encap23" => Self::Encap23, | ||
| "encap34" => Self::Encap34, | ||
| "vlan-src-mac" => Self::VlanSrcMac, | ||
| _ => { | ||
| return Err(DecodeError::from(format!( | ||
| "unknown bond xmit hash policy: {s}" | ||
| ))) | ||
| } | ||
| }) | ||
| } | ||
|
Comment on lines
+455
to
+469
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"layer2" => Ok(Self::Layer2),
"layer34" => Ok(Self::Layer34),
"layer23" => Ok(Self::Layer23),
"encap23" => Ok(Self::Encap23),
"encap34" => Ok(Self::Encap34),
"vlan-src-mac" => Ok(Self::VlanSrcMac),
_ => Err(DecodeError::from(format!("unknown bond xmit hash policy: {s}"))),
}
} |
||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] | ||
| #[non_exhaustive] | ||
| pub enum BondArpAllTargets { | ||
|
|
@@ -432,6 +511,22 @@ impl std::fmt::Display for BondArpAllTargets { | |
| } | ||
| } | ||
|
|
||
| impl std::str::FromStr for BondArpAllTargets { | ||
| type Err = DecodeError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| Ok(match s { | ||
| "any" => Self::Any, | ||
| "all" => Self::All, | ||
| _ => { | ||
| return Err(DecodeError::from(format!( | ||
| "unknown bond arp all targets: {s}" | ||
| ))) | ||
| } | ||
| }) | ||
| } | ||
|
Comment on lines
+517
to
+527
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"any" => Ok(Self::Any),
"all" => Ok(Self::All),
_ => Err(DecodeError::from(format!("unknown bond arp all targets: {s}"))),
}
} |
||
| } | ||
|
|
||
| #[derive(Debug, Clone, Copy, Eq, PartialEq, Default)] | ||
| #[non_exhaustive] | ||
| pub enum BondFailOverMac { | ||
|
|
@@ -478,6 +573,23 @@ impl std::fmt::Display for BondFailOverMac { | |
| } | ||
| } | ||
|
|
||
| impl std::str::FromStr for BondFailOverMac { | ||
| type Err = DecodeError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| Ok(match s { | ||
| "none" => Self::None, | ||
| "active" => Self::Active, | ||
| "follow" => Self::Follow, | ||
| _ => { | ||
| return Err(DecodeError::from(format!( | ||
| "unknown bond fail over mac: {s}" | ||
| ))) | ||
| } | ||
| }) | ||
| } | ||
|
Comment on lines
+579
to
+590
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"none" => Ok(Self::None),
"active" => Ok(Self::Active),
"follow" => Ok(Self::Follow),
_ => Err(DecodeError::from(format!("unknown bond fail over mac: {s}"))),
}
} |
||
| } | ||
|
|
||
| // Some attributes (ARP_IP_TARGET, NS_IP6_TARGET) contain a nested | ||
| // list of IP addresses, where each element uses the index as NLA kind | ||
| // and the address as value. InfoBond exposes vectors of IP addresses, | ||
|
|
@@ -643,6 +755,37 @@ impl From<BondAdSelect> for u8 { | |
| } | ||
| } | ||
|
|
||
| impl std::fmt::Display for BondAdSelect { | ||
| fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
| let kernel_name = match self { | ||
| BondAdSelect::Stable => "stable", | ||
| BondAdSelect::Bandwidth => "bandwidth", | ||
| BondAdSelect::Count => "count", | ||
| BondAdSelect::Other(d) => { | ||
| return write!(f, "unknown-variant ({d})") | ||
| } | ||
| }; | ||
| f.write_str(kernel_name) | ||
| } | ||
| } | ||
|
|
||
| impl std::str::FromStr for BondAdSelect { | ||
| type Err = DecodeError; | ||
|
|
||
| fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
| Ok(match s { | ||
| "stable" => Self::Stable, | ||
| "bandwidth" => Self::Bandwidth, | ||
| "count" => Self::Count, | ||
| _ => { | ||
| return Err(DecodeError::from(format!( | ||
| "unknown bond ad select: {s}" | ||
| ))) | ||
| } | ||
| }) | ||
| } | ||
|
Comment on lines
+775
to
+786
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"stable" => Ok(Self::Stable),
"bandwidth" => Ok(Self::Bandwidth),
"count" => Ok(Self::Count),
_ => Err(DecodeError::from(format!("unknown bond ad select: {s}"))),
}
} |
||
| } | ||
|
|
||
| #[derive(Debug, PartialEq, Eq, Clone)] | ||
| #[non_exhaustive] | ||
| pub enum InfoBond { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
Ok(match s { ... _ => return Err(...) })is non-idiomatic in Rust. It is cleaner and more idiomatic to match directly toResult<Self, Self::Err>by wrapping each successful arm inOkand the fallback arm inErr.