use std::{io, result};
use thiserror::Error;
pub type Result<T> = result::Result<T, Error>;
#[derive(Error, Debug)]
pub enum Error {
#[error("fatal unix error encountered: {source}")]
Unix {
source: nix::errno::Errno,
},
#[error("ioctl failed with unexpected return code: got '{0}'")]
UnixIoctl(i32),
#[error("filesystem operation failed on '{path}': {source}")]
FS {
path: String,
source: io::Error,
},
#[error("input/output operation failed")]
IO {
#[from]
source: io::Error,
},
#[error("invalid queue descriptor specified '{0}' is out of range")]
InvalidQueue(usize),
#[error("invalid number of queues specified must be greater than 0")]
InvalidNumQueues,
#[error(
"invalid device name '{name}' is either longer than {max_size}B or the encoding is invalid"
)]
InvalidName {
name: String,
max_size: usize,
},
}
impl Error {
pub fn errno() -> Self {
Self::from(nix::errno::Errno::last())
}
pub fn into_io(self) -> std::io::Error {
use std::io::ErrorKind;
match self {
Self::FS { source, .. } => source,
Self::IO { source } => source,
Self::Unix { source } => std::io::Error::from_raw_os_error(source as i32),
err => std::io::Error::new(ErrorKind::Other, err),
}
}
}
impl From<i32> for Error {
fn from(i: i32) -> Self {
Self::UnixIoctl(i)
}
}
impl From<usize> for Error {
fn from(i: usize) -> Self {
Self::InvalidQueue(i)
}
}
impl From<nix::errno::Errno> for Error {
fn from(e: nix::errno::Errno) -> Self {
Self::Unix { source: e }
}
}