Skip to main content

wireforge_io/
error.rs

1/// I/O error type for wireforge-io.
2#[derive(Debug)]
3pub enum IoError {
4    /// Underlying socket error.
5    Socket(std::io::Error),
6    /// Packet parse failure.
7    Parse(&'static str),
8    /// Operation timed out.
9    Timeout,
10    /// Invalid pcap magic number.
11    InvalidMagic(u32),
12    /// Unsupported pcap/pcapng version.
13    UnsupportedVersion { major: u16, minor: u16 },
14    /// Truncated pcap record.
15    TruncatedRecord { expected: u32, got: usize },
16    /// Specified interface does not exist.
17    InterfaceNotFound(String),
18    /// Permission denied (root or Administrator required).
19    PermissionDenied,
20    /// BPF-specific error (macOS/BSD).
21    Bpf(&'static str),
22    /// NPcap-specific error (Windows).
23    Npcap(String),
24    /// Operation not supported on the current platform.
25    UnsupportedPlatform,
26}
27
28impl core::fmt::Display for IoError {
29    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
30        match self {
31            Self::Socket(e) => write!(f, "socket error: {e}"),
32            Self::Parse(msg) => write!(f, "parse error: {msg}"),
33            Self::Timeout => write!(f, "timeout"),
34            Self::InvalidMagic(m) => write!(f, "invalid pcap magic number: 0x{m:08X}"),
35            Self::UnsupportedVersion { major, minor } => {
36                write!(f, "unsupported pcap version: {major}.{minor}")
37            }
38            Self::TruncatedRecord { expected, got } => {
39                write!(f, "truncated pcap record: expected {expected} bytes, got {got}")
40            }
41            Self::InterfaceNotFound(name) => write!(f, "interface not found: {name}"),
42            Self::PermissionDenied => write!(f, "permission denied (root/Administrator required)"),
43            Self::Bpf(msg) => write!(f, "BPF error: {msg}"),
44            Self::Npcap(msg) => write!(f, "NPcap error: {msg}"),
45            Self::UnsupportedPlatform => write!(f, "operation not supported on this platform"),
46        }
47    }
48}
49
50impl std::error::Error for IoError {}
51
52impl From<std::io::Error> for IoError {
53    fn from(e: std::io::Error) -> Self {
54        IoError::Socket(e)
55    }
56}