use std::io;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("I/O error: {0}")]
Io(#[from] io::Error),
#[error("invalid netlink message: {0}")]
InvalidMessage(String),
#[error("netlink error: {message} (errno: {errno})")]
Netlink {
errno: i32,
message: String,
},
#[error("unsupported address family: {0}")]
UnsupportedFamily(u8),
#[error("unsupported protocol: {0}")]
UnsupportedProtocol(u8),
#[error("buffer too small: need {needed} bytes, have {have}")]
BufferTooSmall {
needed: usize,
have: usize,
},
#[error("parse error: {0}")]
Parse(String),
}
impl Error {
pub fn from_errno(errno: i32) -> Self {
let message = match errno {
libc::EPERM => "Operation not permitted".to_string(),
libc::ENOENT => "No such socket".to_string(),
libc::EINTR => "Interrupted system call".to_string(),
libc::EIO => "I/O error".to_string(),
libc::ENOMEM => "Out of memory".to_string(),
libc::EACCES => "Permission denied".to_string(),
libc::EBUSY => "Device or resource busy".to_string(),
libc::ENODEV => "No such device".to_string(),
libc::EINVAL => "Invalid argument".to_string(),
libc::EOPNOTSUPP => "Operation not supported on socket".to_string(),
_ => format!("Unknown error {}", errno),
};
Error::Netlink { errno, message }
}
}