Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/link/link_info/infos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,47 @@ impl Nla for InfoKind {
}
}

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()),
})
}
}
Comment on lines +305 to +344

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

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


impl<'a, T: AsRef<[u8]> + ?Sized> Parseable<NlaBuffer<&'a T>> for InfoKind {
fn parse(buf: &NlaBuffer<&'a T>) -> Result<InfoKind, DecodeError> {
if buf.kind() != IFLA_INFO_KIND {
Expand Down
Loading