impl FromStr for NeighbourState#260
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
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.
| 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) |
There was a problem hiding this comment.
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.
| 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
- 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.
There was a problem hiding this comment.
@cathay4t Personally I find this suggestion less readable and more repetitive.
Do you have a preference?
There was a problem hiding this comment.
How about From<&str> and store unknown to Self::Other?
There was a problem hiding this comment.
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")?
There was a problem hiding this comment.
I see. Then DecodeError it should be
There was a problem hiding this comment.
No problem, updated.
7342557 to
ef90458
Compare
96b25d7 to
7362852
Compare
7362852 to
83a90e2
Compare
|
Thanks for the contribution! Will merge once CI is happy. |
No description provided.