use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub enum ErrorKind {
NotFound,
PermissionDenied,
ConnectionRefused,
ConnectionReset,
HostUnreachable,
NetworkUnreachable,
ConnectionAborted,
NotConnected,
AddrInUse,
AddrNotAvailable,
NetworkDown,
BrokenPipe,
AlreadyExists,
WouldBlock,
NotADirectory,
IsADirectory,
DirectoryNotEmpty,
ReadOnlyFilesystem,
StaleNetworkFileHandle,
InvalidInput,
InvalidData,
TimedOut,
WriteZero,
StorageFull,
NotSeekable,
QuotaExceeded,
ResourceBusy,
ExecutableFileBusy,
Deadlock,
CrossesDevices,
TooManyLinks,
InvalidFilename,
ArgumentListTooLong,
Interrupted,
Unsupported,
UnexpectedEof,
OutOfMemory,
Other,
}
impl ErrorKind {
pub const NOT_FOUND: &str = "not_found";
pub const PERMISSION_DENIED: &str = "permission_denied";
pub const CONNECTION_REFUSED: &str = "connection_refused";
pub const CONNECTION_RESET: &str = "connection_reset";
pub const HOST_UNREACHABLE: &str = "host_unreachable";
pub const NETWORK_UNREACHABLE: &str = "network_unreachable";
pub const CONNECTION_ABORTED: &str = "connection_aborted";
pub const NOT_CONNECTED: &str = "not_connected";
pub const ADDR_IN_USE: &str = "addr_in_use";
pub const ADDR_NOT_AVAILABLE: &str = "addr_not_available";
pub const NETWORK_DOWN: &str = "network_down";
pub const BROKEN_PIPE: &str = "broken_pipe";
pub const ALREADY_EXISTS: &str = "already_exists";
pub const WOULD_BLOCK: &str = "would_block";
pub const NOT_A_DIRECTORY: &str = "not_a_directory";
pub const IS_A_DIRECTORY: &str = "is_a_directory";
pub const DIRECTORY_NOT_EMPTY: &str = "directory_not_empty";
pub const READ_ONLY_FILESYSTEM: &str = "read_only_filesystem";
pub const STALE_NETWORK_FILE_HANDLE: &str = "stale_network_file_handle";
pub const INVALID_INPUT: &str = "invalid_input";
pub const INVALID_DATA: &str = "invalid_data";
pub const TIMED_OUT: &str = "timed_out";
pub const WRITE_ZERO: &str = "write_zero";
pub const STORAGE_FULL: &str = "storage_full";
pub const NOT_SEEKABLE: &str = "not_seekable";
pub const QUOTA_EXCEEDED: &str = "quota_exceeded";
pub const RESOURCE_BUSY: &str = "resource_busy";
pub const EXECUTABLE_FILE_BUSY: &str = "executable_file_busy";
pub const DEADLOCK: &str = "deadlock";
pub const CROSSES_DEVICES: &str = "crosses_devices";
pub const TOO_MANY_LINKS: &str = "too_many_links";
pub const INVALID_FILENAME: &str = "invalid_filename";
pub const ARGUMENT_LIST_TOO_LONG: &str = "argument_list_too_long";
pub const INTERRUPTED: &str = "interrupted";
pub const UNSUPPORTED: &str = "unsupported";
pub const UNEXPECTED_EOF: &str = "unexpected_eof";
pub const OUT_OF_MEMORY: &str = "out_of_memory";
pub const OTHER: &str = "other";
pub const fn new() -> Self {
Self::Other
}
pub const fn from_io(err: std::io::ErrorKind) -> Self {
match err {
std::io::ErrorKind::NotFound => Self::NotFound,
std::io::ErrorKind::PermissionDenied => Self::PermissionDenied,
std::io::ErrorKind::ConnectionRefused => Self::ConnectionRefused,
std::io::ErrorKind::ConnectionReset => Self::ConnectionReset,
std::io::ErrorKind::HostUnreachable => Self::HostUnreachable,
std::io::ErrorKind::NetworkUnreachable => Self::NetworkUnreachable,
std::io::ErrorKind::ConnectionAborted => Self::ConnectionAborted,
std::io::ErrorKind::NotConnected => Self::NotConnected,
std::io::ErrorKind::AddrInUse => Self::AddrInUse,
std::io::ErrorKind::AddrNotAvailable => Self::AddrNotAvailable,
std::io::ErrorKind::NetworkDown => Self::NetworkDown,
std::io::ErrorKind::BrokenPipe => Self::BrokenPipe,
std::io::ErrorKind::AlreadyExists => Self::AlreadyExists,
std::io::ErrorKind::WouldBlock => Self::WouldBlock,
std::io::ErrorKind::NotADirectory => Self::NotADirectory,
std::io::ErrorKind::IsADirectory => Self::IsADirectory,
std::io::ErrorKind::DirectoryNotEmpty => Self::DirectoryNotEmpty,
std::io::ErrorKind::ReadOnlyFilesystem => Self::ReadOnlyFilesystem,
std::io::ErrorKind::StaleNetworkFileHandle => Self::StaleNetworkFileHandle,
std::io::ErrorKind::InvalidInput => Self::InvalidInput,
std::io::ErrorKind::InvalidData => Self::InvalidData,
std::io::ErrorKind::TimedOut => Self::TimedOut,
std::io::ErrorKind::WriteZero => Self::WriteZero,
std::io::ErrorKind::StorageFull => Self::StorageFull,
std::io::ErrorKind::NotSeekable => Self::NotSeekable,
std::io::ErrorKind::QuotaExceeded => Self::QuotaExceeded,
std::io::ErrorKind::ResourceBusy => Self::ResourceBusy,
std::io::ErrorKind::ExecutableFileBusy => Self::ExecutableFileBusy,
std::io::ErrorKind::Deadlock => Self::Deadlock,
std::io::ErrorKind::CrossesDevices => Self::CrossesDevices,
std::io::ErrorKind::TooManyLinks => Self::TooManyLinks,
std::io::ErrorKind::InvalidFilename => Self::InvalidFilename,
std::io::ErrorKind::ArgumentListTooLong => Self::ArgumentListTooLong,
std::io::ErrorKind::Interrupted => Self::Interrupted,
std::io::ErrorKind::Unsupported => Self::Unsupported,
std::io::ErrorKind::UnexpectedEof => Self::UnexpectedEof,
std::io::ErrorKind::OutOfMemory => Self::OutOfMemory,
_ => Self::Other,
}
}
pub const fn as_io(&self) -> std::io::ErrorKind {
match self {
Self::NotFound => std::io::ErrorKind::NotFound,
Self::PermissionDenied => std::io::ErrorKind::PermissionDenied,
Self::ConnectionRefused => std::io::ErrorKind::ConnectionRefused,
Self::ConnectionReset => std::io::ErrorKind::ConnectionReset,
Self::HostUnreachable => std::io::ErrorKind::HostUnreachable,
Self::NetworkUnreachable => std::io::ErrorKind::NetworkUnreachable,
Self::ConnectionAborted => std::io::ErrorKind::ConnectionAborted,
Self::NotConnected => std::io::ErrorKind::NotConnected,
Self::AddrInUse => std::io::ErrorKind::AddrInUse,
Self::AddrNotAvailable => std::io::ErrorKind::AddrNotAvailable,
Self::NetworkDown => std::io::ErrorKind::NetworkDown,
Self::BrokenPipe => std::io::ErrorKind::BrokenPipe,
Self::AlreadyExists => std::io::ErrorKind::AlreadyExists,
Self::WouldBlock => std::io::ErrorKind::WouldBlock,
Self::NotADirectory => std::io::ErrorKind::NotADirectory,
Self::IsADirectory => std::io::ErrorKind::IsADirectory,
Self::DirectoryNotEmpty => std::io::ErrorKind::DirectoryNotEmpty,
Self::ReadOnlyFilesystem => std::io::ErrorKind::ReadOnlyFilesystem,
Self::StaleNetworkFileHandle => std::io::ErrorKind::StaleNetworkFileHandle,
Self::InvalidInput => std::io::ErrorKind::InvalidInput,
Self::InvalidData => std::io::ErrorKind::InvalidData,
Self::TimedOut => std::io::ErrorKind::TimedOut,
Self::WriteZero => std::io::ErrorKind::WriteZero,
Self::StorageFull => std::io::ErrorKind::StorageFull,
Self::NotSeekable => std::io::ErrorKind::NotSeekable,
Self::QuotaExceeded => std::io::ErrorKind::QuotaExceeded,
Self::ResourceBusy => std::io::ErrorKind::ResourceBusy,
Self::ExecutableFileBusy => std::io::ErrorKind::ExecutableFileBusy,
Self::Deadlock => std::io::ErrorKind::Deadlock,
Self::CrossesDevices => std::io::ErrorKind::CrossesDevices,
Self::TooManyLinks => std::io::ErrorKind::TooManyLinks,
Self::InvalidFilename => std::io::ErrorKind::InvalidFilename,
Self::ArgumentListTooLong => std::io::ErrorKind::ArgumentListTooLong,
Self::Interrupted => std::io::ErrorKind::Interrupted,
Self::Unsupported => std::io::ErrorKind::Unsupported,
Self::UnexpectedEof => std::io::ErrorKind::UnexpectedEof,
Self::OutOfMemory => std::io::ErrorKind::OutOfMemory,
_ => std::io::ErrorKind::Other,
}
}
pub const fn as_str(&self) -> &'static str {
match self {
Self::NotFound => Self::NOT_FOUND,
Self::PermissionDenied => Self::PERMISSION_DENIED,
Self::ConnectionRefused => Self::CONNECTION_REFUSED,
Self::ConnectionReset => Self::CONNECTION_RESET,
Self::HostUnreachable => Self::HOST_UNREACHABLE,
Self::NetworkUnreachable => Self::NETWORK_UNREACHABLE,
Self::ConnectionAborted => Self::CONNECTION_ABORTED,
Self::NotConnected => Self::NOT_CONNECTED,
Self::AddrInUse => Self::ADDR_IN_USE,
Self::AddrNotAvailable => Self::ADDR_NOT_AVAILABLE,
Self::NetworkDown => Self::NETWORK_DOWN,
Self::BrokenPipe => Self::BROKEN_PIPE,
Self::AlreadyExists => Self::ALREADY_EXISTS,
Self::WouldBlock => Self::WOULD_BLOCK,
Self::NotADirectory => Self::NOT_A_DIRECTORY,
Self::IsADirectory => Self::IS_A_DIRECTORY,
Self::DirectoryNotEmpty => Self::DIRECTORY_NOT_EMPTY,
Self::ReadOnlyFilesystem => Self::READ_ONLY_FILESYSTEM,
Self::StaleNetworkFileHandle => Self::STALE_NETWORK_FILE_HANDLE,
Self::InvalidInput => Self::INVALID_INPUT,
Self::InvalidData => Self::INVALID_DATA,
Self::TimedOut => Self::TIMED_OUT,
Self::WriteZero => Self::WRITE_ZERO,
Self::StorageFull => Self::STORAGE_FULL,
Self::NotSeekable => Self::NOT_SEEKABLE,
Self::QuotaExceeded => Self::QUOTA_EXCEEDED,
Self::ResourceBusy => Self::RESOURCE_BUSY,
Self::ExecutableFileBusy => Self::EXECUTABLE_FILE_BUSY,
Self::Deadlock => Self::DEADLOCK,
Self::CrossesDevices => Self::CROSSES_DEVICES,
Self::TooManyLinks => Self::TOO_MANY_LINKS,
Self::InvalidFilename => Self::INVALID_FILENAME,
Self::ArgumentListTooLong => Self::ARGUMENT_LIST_TOO_LONG,
Self::Interrupted => Self::INTERRUPTED,
Self::Unsupported => Self::UNSUPPORTED,
Self::UnexpectedEof => Self::UNEXPECTED_EOF,
Self::OutOfMemory => Self::OUT_OF_MEMORY,
Self::Other => Self::OTHER,
}
}
}
impl Default for ErrorKind {
fn default() -> Self {
Self::new()
}
}
impl From<std::io::ErrorKind> for ErrorKind {
fn from(err: std::io::ErrorKind) -> Self {
Self::from_io(err)
}
}
impl From<ErrorKind> for std::io::ErrorKind {
fn from(err: ErrorKind) -> Self {
err.as_io()
}
}
impl std::fmt::Display for ErrorKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}