Skip to content

impl FromStr for NeighbourState#260

Merged
cathay4t merged 1 commit into
rust-netlink:mainfrom
Gilnaa:neighbour-type-from-str
May 28, 2026
Merged

impl FromStr for NeighbourState#260
cathay4t merged 1 commit into
rust-netlink:mainfrom
Gilnaa:neighbour-type-from-str

Conversation

@Gilnaa

@Gilnaa Gilnaa commented May 24, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@codecov

codecov Bot commented May 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 68.01%. Comparing base (1fb4bd7) to head (83a90e2).
⚠️ Report is 47 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #260      +/-   ##
==========================================
- Coverage   68.10%   68.01%   -0.10%     
==========================================
  Files         144      146       +2     
  Lines       10103    10320     +217     
==========================================
+ Hits         6881     7019     +138     
- Misses       3222     3301      +79     

☔ 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.

@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 the FromStr trait for NeighbourState, enabling the parsing of neighbor state strings into their respective enum variants, and introduces a NeighbourStateParseError struct. Feedback suggests making the error struct more idiomatic by implementing standard traits like Debug, Display, and Error for better ecosystem compatibility. Additionally, the from_str implementation can be simplified by returning the result of the match expression directly to improve code conciseness and follow Rust idioms.

Comment thread src/neighbour/state.rs Outdated
Comment thread src/neighbour/state.rs
Comment on lines +86 to +99
let state = match state {
"incomplete" => Self::Incomplete,
"reachable" => Self::Reachable,
"stale" => Self::Stale,
"delay" => Self::Delay,
"probe" => Self::Probe,
"failed" => Self::Failed,
"noarp" => Self::Noarp,
"permanent" => Self::Permanent,
"none" => Self::None,
_ => return Err(NeighbourStateParseError),
};

Ok(state)

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

The from_str implementation can be simplified by returning the Result directly from the match expression. This avoids unnecessary variable shadowing and makes the code more concise and idiomatic. Ensure the formatting adheres to the project's rustfmt configuration regarding line length.

Suggested change
let state = match state {
"incomplete" => Self::Incomplete,
"reachable" => Self::Reachable,
"stale" => Self::Stale,
"delay" => Self::Delay,
"probe" => Self::Probe,
"failed" => Self::Failed,
"noarp" => Self::Noarp,
"permanent" => Self::Permanent,
"none" => Self::None,
_ => return Err(NeighbourStateParseError),
};
Ok(state)
match state {
"incomplete" => Ok(Self::Incomplete),
"reachable" => Ok(Self::Reachable),
"stale" => Ok(Self::Stale),
"delay" => Ok(Self::Delay),
"probe" => Ok(Self::Probe),
"failed" => Ok(Self::Failed),
"noarp" => Ok(Self::Noarp),
"permanent" => Ok(Self::Permanent),
"none" => Ok(Self::None),
_ => Err(NeighbourStateParseError),
}
References
  1. Code formatting must adhere to the project's rustfmt configuration, especially the maximum line length. This may require using block expressions for what would otherwise be single-line match arms to stay within the configured limit.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@cathay4t Personally I find this suggestion less readable and more repetitive.
Do you have a preference?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about From<&str> and store unknown to Self::Other?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Self::Other can only store a u16, but the user can input anything into From<&str>, which cannot fail.
What do we return from NeighbourState::from("not really anything")?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see. Then DecodeError it should be

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No problem, updated.

@Gilnaa Gilnaa force-pushed the neighbour-type-from-str branch from 7342557 to ef90458 Compare May 24, 2026 07:06
Comment thread src/neighbour/state.rs Outdated
@Gilnaa Gilnaa force-pushed the neighbour-type-from-str branch 3 times, most recently from 96b25d7 to 7362852 Compare May 28, 2026 06:27
@cathay4t cathay4t force-pushed the neighbour-type-from-str branch from 7362852 to 83a90e2 Compare May 28, 2026 10:20
@cathay4t cathay4t enabled auto-merge (rebase) May 28, 2026 10:20
@cathay4t

Copy link
Copy Markdown
Member

Thanks for the contribution! Will merge once CI is happy.

@cathay4t cathay4t merged commit b3abdf8 into rust-netlink:main May 28, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants