Add TryFrom<&str> For InfoKind#261
Conversation
Signed-off-by: Gris Ge <cnfourt@gmail.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #261 +/- ##
==========================================
- Coverage 68.10% 67.66% -0.45%
==========================================
Files 144 146 +2
Lines 10103 10316 +213
==========================================
+ Hits 6881 6980 +99
- Misses 3222 3336 +114 ☔ 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 TryFrom<&str> trait for the InfoKind enum to allow for conversion from string slices. The reviewer suggests that since the conversion is infallible due to a catch-all match arm, it is more idiomatic to implement the From<&str> trait instead. This would simplify the code by removing unnecessary Result wrapping and provide a more standard interface for the conversion.
| impl TryFrom<&str> for InfoKind { | ||
| type Error = DecodeError; | ||
|
|
||
| fn try_from(s: &str) -> Result<Self, Self::Error> { | ||
| Ok(match s { | ||
| DUMMY => Self::Dummy, | ||
| IFB => Self::Ifb, | ||
| BRIDGE => Self::Bridge, | ||
| TUN => Self::Tun, | ||
| NLMON => Self::Nlmon, | ||
| VLAN => Self::Vlan, | ||
| VETH => Self::Veth, | ||
| VXLAN => Self::Vxlan, | ||
| BOND => Self::Bond, | ||
| IPVLAN => Self::IpVlan, | ||
| IPVTAP => Self::IpVtap, | ||
| MACVLAN => Self::MacVlan, | ||
| MACVTAP => Self::MacVtap, | ||
| GRETAP => Self::GreTap, | ||
| IP6GRETAP => Self::GreTap6, | ||
| IPIP => Self::IpIp, | ||
| IP6TNL => Self::Ip6Tnl, | ||
| SIT => Self::SitTun, | ||
| GRE => Self::GreTun, | ||
| IP6GRE => Self::GreTun6, | ||
| VTI => Self::Vti, | ||
| VRF => Self::Vrf, | ||
| GTP => Self::Gtp, | ||
| IPOIB => Self::Ipoib, | ||
| WIREGUARD => Self::Wireguard, | ||
| XFRM => Self::Xfrm, | ||
| MACSEC => Self::MacSec, | ||
| HSR => Self::Hsr, | ||
| GENEVE => Self::Geneve, | ||
| NETKIT => Self::Netkit, | ||
| VXCAN => Self::Vxcan, | ||
| _ => Self::Other(s.to_owned()), | ||
| }) | ||
| } | ||
| } |
There was a problem hiding this comment.
Since the conversion from &str to InfoKind is infallible (the catch-all _ arm handles all cases by returning Self::Other), it is more idiomatic to implement the From<&str> trait instead of TryFrom<&str>. TryFrom is intended for conversions that can fail, whereas From is preferred for guaranteed conversions. This change also simplifies the code by removing the unnecessary Result wrapping. Additionally, once this trait is implemented, the Parseable::parse implementation for InfoKind can be refactored to use it, reducing code duplication.
impl From<&str> for InfoKind {
fn from(s: &str) -> Self {
match s {
DUMMY => Self::Dummy,
IFB => Self::Ifb,
BRIDGE => Self::Bridge,
TUN => Self::Tun,
NLMON => Self::Nlmon,
VLAN => Self::Vlan,
VETH => Self::Veth,
VXLAN => Self::Vxlan,
BOND => Self::Bond,
IPVLAN => Self::IpVlan,
IPVTAP => Self::IpVtap,
MACVLAN => Self::MacVlan,
MACVTAP => Self::MacVtap,
GRETAP => Self::GreTap,
IP6GRETAP => Self::GreTap6,
IPIP => Self::IpIp,
IP6TNL => Self::Ip6Tnl,
SIT => Self::SitTun,
GRE => Self::GreTun,
IP6GRE => Self::GreTun6,
VTI => Self::Vti,
VRF => Self::Vrf,
GTP => Self::Gtp,
IPOIB => Self::Ipoib,
WIREGUARD => Self::Wireguard,
XFRM => Self::Xfrm,
MACSEC => Self::MacSec,
HSR => Self::Hsr,
GENEVE => Self::Geneve,
NETKIT => Self::Netkit,
VXCAN => Self::Vxcan,
_ => Self::Other(s.to_owned()),
}
}
}
No description provided.