wireguard_uapi/linux/
interface.rs1use crate::linux::attr::WgDeviceAttribute;
2use crate::linux::consts::NLA_NETWORK_ORDER;
3use neli::{err::NlError, genl::Nlattr, types::Buffer};
4use std::borrow::Cow;
5use std::convert::TryFrom;
6
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub enum DeviceInterface<'a> {
9 Index(u32),
10 Name(Cow<'a, str>),
11}
12
13impl<'a> DeviceInterface<'a> {
14 pub fn from_index(index: u32) -> Self {
15 DeviceInterface::Index(index)
16 }
17
18 pub fn from_name<T: Into<Cow<'a, str>>>(name: T) -> Self {
19 DeviceInterface::Name(name.into())
20 }
21}
22
23impl TryFrom<&DeviceInterface<'_>> for Nlattr<WgDeviceAttribute, Buffer> {
24 type Error = NlError;
25
26 fn try_from(interface: &DeviceInterface) -> Result<Self, Self::Error> {
27 let attr = match interface {
28 &DeviceInterface::Index(ifindex) => Nlattr::new(
29 false,
30 NLA_NETWORK_ORDER,
31 WgDeviceAttribute::Ifindex,
32 ifindex,
33 )?,
34 DeviceInterface::Name(ifname) => Nlattr::new(
35 false,
36 NLA_NETWORK_ORDER,
37 WgDeviceAttribute::Ifname,
38 ifname.as_ref(),
39 )?,
40 };
41 Ok(attr)
42 }
43}