Skip to content

link: bond: implement FromStr for bond enums#263

Merged
cathay4t merged 1 commit into
rust-netlink:mainfrom
cathay4t:main
Jun 1, 2026
Merged

link: bond: implement FromStr for bond enums#263
cathay4t merged 1 commit into
rust-netlink:mainfrom
cathay4t:main

Conversation

@cathay4t

@cathay4t cathay4t commented Jun 1, 2026

Copy link
Copy Markdown
Member

Introduced support for parsing bond option enums from string:

  • BondMode
  • BondArpValidate
  • BondArpAllTargets
  • BondPrimaryReselect
  • BondFailOverMac
  • BondXmitHashPolicy
  • BondAdSelect

All conversions use DecodeError as the error type.

Unit tests are included for all seven FromStr implementations.

Introduced support for parsing bond option enums from string:
 * `BondMode`
 * `BondArpValidate`
 * `BondArpAllTargets`
 * `BondPrimaryReselect`
 * `BondFailOverMac`
 * `BondXmitHashPolicy`
 * `BondAdSelect`

All conversions use DecodeError as the error type.

Unit tests are included for all seven FromStr implementations.

Signed-off-by: Gris Ge <cnfourt@gmail.com>
@codecov

codecov Bot commented Jun 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.02439% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.17%. Comparing base (1fb4bd7) to head (a2ab9a7).
⚠️ Report is 47 commits behind head on main.

Files with missing lines Patch % Lines
src/link/link_info/bond.rs 89.02% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #263      +/-   ##
==========================================
+ Coverage   68.10%   68.17%   +0.07%     
==========================================
  Files         144      146       +2     
  Lines       10103    10402     +299     
==========================================
+ Hits         6881     7092     +211     
- Misses       3222     3310      +88     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@cathay4t cathay4t merged commit 192cb0c into rust-netlink:main Jun 1, 2026
11 checks passed

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Code Review

This pull request implements std::str::FromStr for several bond-related enums, including BondMode, BondArpValidate, BondPrimaryReselect, BondXmitHashPolicy, BondArpAllTargets, BondFailOverMac, and BondAdSelect, along with corresponding unit tests. The reviewer noted that the from_str implementations use a non-idiomatic Rust pattern by wrapping the entire match block in Ok and using an early return for errors. It is recommended to match directly to Result<Self, Self::Err> by wrapping each successful arm in Ok and the fallback arm in Err.

Comment on lines +230 to +245
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}"
)))
}
})
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using Ok(match s { ... _ => return Err(...) }) is non-idiomatic in Rust. It is cleaner and more idiomatic to match directly to Result<Self, Self::Err> by wrapping each successful arm in Ok and the fallback arm in Err.

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "balance-rr" => Ok(Self::BalanceRr),
            "active-backup" => Ok(Self::ActiveBackup),
            "balance-xor" => Ok(Self::BalanceXor),
            "broadcast" => Ok(Self::Broadcast),
            "802.3ad" => Ok(Self::Ieee8023Ad),
            "balance-tlb" => Ok(Self::BalanceTlb),
            "balance-alb" => Ok(Self::BalanceAlb),
            _ => Err(DecodeError::from(format!("unknown bond mode: {s}"))),
        }
    }

Comment on lines +313 to +328
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}"
)))
}
})
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using Ok(match s { ... _ => return Err(...) }) is non-idiomatic in Rust. It is cleaner and more idiomatic to match directly to Result<Self, Self::Err> by wrapping each successful arm in Ok and the fallback arm in Err.

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

Comment on lines +380 to +391
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}"
)))
}
})
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using Ok(match s { ... _ => return Err(...) }) is non-idiomatic in Rust. It is cleaner and more idiomatic to match directly to Result<Self, Self::Err> by wrapping each successful arm in Ok and the fallback arm in Err.

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

Comment on lines +455 to +469
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}"
)))
}
})
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using Ok(match s { ... _ => return Err(...) }) is non-idiomatic in Rust. It is cleaner and more idiomatic to match directly to Result<Self, Self::Err> by wrapping each successful arm in Ok and the fallback arm in Err.

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

Comment on lines +517 to +527
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}"
)))
}
})
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using Ok(match s { ... _ => return Err(...) }) is non-idiomatic in Rust. It is cleaner and more idiomatic to match directly to Result<Self, Self::Err> by wrapping each successful arm in Ok and the fallback arm in Err.

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

Comment on lines +579 to +590
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}"
)))
}
})
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using Ok(match s { ... _ => return Err(...) }) is non-idiomatic in Rust. It is cleaner and more idiomatic to match directly to Result<Self, Self::Err> by wrapping each successful arm in Ok and the fallback arm in Err.

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

Comment on lines +775 to +786
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}"
)))
}
})
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using Ok(match s { ... _ => return Err(...) }) is non-idiomatic in Rust. It is cleaner and more idiomatic to match directly to Result<Self, Self::Err> by wrapping each successful arm in Ok and the fallback arm in Err.

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

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant