use std::fmt;
use crate::ffi;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct InterfaceIndex(u32);
impl InterfaceIndex {
pub fn from_raw(ndx: u32) -> Option<Self> {
match ndx {
ffi::INTERFACE_INDEX_ANY => None,
ffi::INTERFACE_INDEX_LOCAL_ONLY => None,
ffi::INTERFACE_INDEX_UNICAST => None,
ffi::INTERFACE_INDEX_P2P => None,
_ => Some(Self(ndx)),
}
}
pub fn into_raw(self) -> u32 {
self.0
}
}
impl From<InterfaceIndex> for u32 {
fn from(ndx: InterfaceIndex) -> Self {
ndx.into_raw()
}
}
impl fmt::Debug for InterfaceIndex {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[non_exhaustive]
pub enum Interface {
Any,
Index(InterfaceIndex),
LocalOnly,
Unicast,
PeerToPeer,
}
impl Default for Interface {
fn default() -> Self {
Self::Any
}
}
impl Interface {
pub fn from_raw(raw: u32) -> Self {
match raw {
ffi::INTERFACE_INDEX_ANY => Self::Any,
ffi::INTERFACE_INDEX_LOCAL_ONLY => Self::LocalOnly,
ffi::INTERFACE_INDEX_UNICAST => Self::Unicast,
ffi::INTERFACE_INDEX_P2P => Self::PeerToPeer,
_ => Self::Index(InterfaceIndex(raw)),
}
}
pub fn into_raw(self) -> u32 {
match self {
Self::Any => ffi::INTERFACE_INDEX_ANY,
Self::Index(InterfaceIndex(raw)) => raw,
Self::LocalOnly => ffi::INTERFACE_INDEX_LOCAL_ONLY,
Self::Unicast => ffi::INTERFACE_INDEX_UNICAST,
Self::PeerToPeer => ffi::INTERFACE_INDEX_P2P,
}
}
pub fn scope_id(self) -> u32 {
match self {
Self::Index(InterfaceIndex(scope_id)) => scope_id,
_ => 0,
}
}
}
impl From<Interface> for u32 {
fn from(i: Interface) -> u32 {
i.into_raw()
}
}