use super::constants::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
#[repr(u16)]
pub enum ArpOperation {
Request = ARP_OP_REQUEST,
Reply = ARP_OP_REPLY,
RarpRequest = ARP_OP_RARP_REQUEST,
RarpReply = ARP_OP_RARP_REPLY,
DrarpRequest = ARP_OP_DRARP_REQUEST,
DrarpReply = ARP_OP_DRARP_REPLY,
DrarpError = ARP_OP_DRARP_ERROR,
InArpRequest = ARP_OP_INARP_REQUEST,
InArpReply = ARP_OP_INARP_REPLY,
ArpNak = ARP_OP_ARP_NAK,
MaposUnarp = ARP_OP_MAPOS_UNARP,
}
impl ArpOperation {
pub fn from_opcode(opcode: u16) -> Option<Self> {
match opcode {
ARP_OP_REQUEST => Some(Self::Request),
ARP_OP_REPLY => Some(Self::Reply),
ARP_OP_RARP_REQUEST => Some(Self::RarpRequest),
ARP_OP_RARP_REPLY => Some(Self::RarpReply),
ARP_OP_DRARP_REQUEST => Some(Self::DrarpRequest),
ARP_OP_DRARP_REPLY => Some(Self::DrarpReply),
ARP_OP_DRARP_ERROR => Some(Self::DrarpError),
ARP_OP_INARP_REQUEST => Some(Self::InArpRequest),
ARP_OP_INARP_REPLY => Some(Self::InArpReply),
ARP_OP_ARP_NAK => Some(Self::ArpNak),
ARP_OP_MAPOS_UNARP => Some(Self::MaposUnarp),
_ => None,
}
}
pub fn opcode(self) -> u16 {
self as u16
}
pub fn label(self) -> &'static str {
match self {
Self::Request => "request",
Self::Reply => "reply",
Self::RarpRequest => "rarp-request",
Self::RarpReply => "rarp-reply",
Self::DrarpRequest => "drarp-request",
Self::DrarpReply => "drarp-reply",
Self::DrarpError => "drarp-error",
Self::InArpRequest => "inarp-request",
Self::InArpReply => "inarp-reply",
Self::ArpNak => "arp-nak",
Self::MaposUnarp => "mapos-unarp",
}
}
}
impl From<ArpOperation> for u16 {
fn from(value: ArpOperation) -> Self {
value as u16
}
}
impl TryFrom<u16> for ArpOperation {
type Error = u16;
fn try_from(value: u16) -> core::result::Result<Self, Self::Error> {
Self::from_opcode(value).ok_or(value)
}
}