mutnet/arp/
error.rs

1//! ARP specific errors.
2
3use core::error;
4use core::fmt::{Debug, Display, Formatter};
5
6use crate::error::UnexpectedBufferEndError;
7
8/// Error returned when parsing an ARP header.
9#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
10pub enum ParseArpError {
11    /// The data buffer ended unexpectedly.
12    UnexpectedBufferEnd(UnexpectedBufferEndError),
13    /// The hardware or protocol type is not supported.
14    UnsupportedHardwareOrProtocolFields,
15    /// The operation code is not supported
16    UnsupportedOperationCode {
17        /// The operation code found.
18        operation_code: u16,
19    },
20}
21
22impl From<UnexpectedBufferEndError> for ParseArpError {
23    #[inline]
24    fn from(value: UnexpectedBufferEndError) -> Self {
25        Self::UnexpectedBufferEnd(value)
26    }
27}
28
29impl Display for ParseArpError {
30    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
31        match self {
32            Self::UnsupportedHardwareOrProtocolFields => {
33                write!(
34                    f,
35                    "Hardware/protocol type or length do not fit IPv4 over ethernet"
36                )
37            }
38            Self::UnexpectedBufferEnd(err) => {
39                write!(f, "{err}")
40            }
41            Self::UnsupportedOperationCode { operation_code } => {
42                write!(
43                    f,
44                    "Unsupported operation code, only request(1) and reply(2) are supported, was: {operation_code}"
45                )
46            }
47        }
48    }
49}
50
51impl error::Error for ParseArpError {}