Skip to main content

netmap_rs/
error.rs

1use std::io;
2use thiserror::Error;
3
4/// Errors that can occur when working with Netmap
5
6#[derive(Error, Debug)]
7pub enum Error {
8    /// I/O error from the underlying system
9    #[error("I/O error: {0}")]
10    Io(#[from] io::Error),
11
12    /// operation would block
13    #[error("Operation would block")]
14    WouldBlock,
15
16    /// binding interface failed
17    #[error("Failed to bind to interface: {0}")]
18    BindFail(String),
19
20    /// Invalid ring index
21    #[error("Invalid ring index: {0}")]
22    InvalidRingIndex(usize),
23
24    /// Packet too large for ring buffer
25    #[error("Packet too large for ring buffer: {0} bytes")]
26    PacketTooLarge(usize),
27
28    /// Not enough space in ring buffer
29    #[error("Not enough space in ring buffer")]
30    InsufficientSpace,
31
32    /// Platform not yet supported
33    #[error("Platform not yet supported: {0}")]
34    UnsupportedPlatform(String),
35
36    /// Feature not  supported in fallback mode
37    #[error("Feature not supported in fallback mode: {0}")]
38    FallbackUnsupported(String),
39}
40
41impl From<Error> for io::Error {
42    fn from(err: Error) -> io::Error {
43        match err {
44            Error::Io(e) => e,
45            e => io::Error::other(e.to_string()),
46        }
47    }
48}
49
50#[cfg(test)]
51mod tests {
52    use super::*;
53
54    #[test]
55    fn display_messages() {
56        assert_eq!(Error::WouldBlock.to_string(), "Operation would block");
57        assert_eq!(
58            Error::BindFail("eth0".into()).to_string(),
59            "Failed to bind to interface: eth0"
60        );
61        assert_eq!(
62            Error::InvalidRingIndex(3).to_string(),
63            "Invalid ring index: 3"
64        );
65        assert_eq!(
66            Error::PacketTooLarge(9001).to_string(),
67            "Packet too large for ring buffer: 9001 bytes"
68        );
69        assert_eq!(
70            Error::InsufficientSpace.to_string(),
71            "Not enough space in ring buffer"
72        );
73        assert_eq!(
74            Error::UnsupportedPlatform("plan9".into()).to_string(),
75            "Platform not yet supported: plan9"
76        );
77        assert_eq!(
78            Error::FallbackUnsupported("fec".into()).to_string(),
79            "Feature not supported in fallback mode: fec"
80        );
81    }
82
83    #[test]
84    fn from_io_error() {
85        let io_err = io::Error::new(io::ErrorKind::NotFound, "file missing");
86        let err: Error = io_err.into();
87        assert!(matches!(err, Error::Io(_)));
88    }
89
90    #[test]
91    fn from_error_to_io_error() {
92        let netmap_err = Error::WouldBlock;
93        let io_err: io::Error = netmap_err.into();
94        assert_eq!(io_err.kind(), io::ErrorKind::Other);
95    }
96
97    #[test]
98    fn io_error_unwrapped() {
99        let source = io::Error::new(io::ErrorKind::ConnectionRefused, "refused");
100        let netmap_err = Error::Io(source);
101        let io_err: io::Error = netmap_err.into();
102        assert_eq!(io_err.kind(), io::ErrorKind::ConnectionRefused);
103        assert_eq!(io_err.to_string(), "refused");
104    }
105
106    #[test]
107    fn error_is_send_sync() {
108        fn assert_send_sync<T: Send + Sync>() {}
109        assert_send_sync::<Error>();
110    }
111}