zulip_api/error/
kind.rs

1use serde::{Deserialize, Serialize};
2
3/// Represents error variants for the library.
4#[derive(Clone, Copy, Debug, Eq, PartialEq, Deserialize, Serialize)]
5pub enum ErrorKind {
6    NotFound,
7    PermissionDenied,
8    ConnectionRefused,
9    ConnectionReset,
10    HostUnreachable,
11    NetworkUnreachable,
12    ConnectionAborted,
13    NotConnected,
14    AddrInUse,
15    AddrNotAvailable,
16    NetworkDown,
17    BrokenPipe,
18    AlreadyExists,
19    WouldBlock,
20    NotADirectory,
21    IsADirectory,
22    DirectoryNotEmpty,
23    ReadOnlyFilesystem,
24    StaleNetworkFileHandle,
25    InvalidInput,
26    InvalidData,
27    TimedOut,
28    WriteZero,
29    StorageFull,
30    NotSeekable,
31    QuotaExceeded,
32    ResourceBusy,
33    ExecutableFileBusy,
34    Deadlock,
35    CrossesDevices,
36    TooManyLinks,
37    InvalidFilename,
38    ArgumentListTooLong,
39    Interrupted,
40    Unsupported,
41    UnexpectedEof,
42    OutOfMemory,
43    Other,
44}
45
46impl ErrorKind {
47    pub const NOT_FOUND: &str = "not_found";
48    pub const PERMISSION_DENIED: &str = "permission_denied";
49    pub const CONNECTION_REFUSED: &str = "connection_refused";
50    pub const CONNECTION_RESET: &str = "connection_reset";
51    pub const HOST_UNREACHABLE: &str = "host_unreachable";
52    pub const NETWORK_UNREACHABLE: &str = "network_unreachable";
53    pub const CONNECTION_ABORTED: &str = "connection_aborted";
54    pub const NOT_CONNECTED: &str = "not_connected";
55    pub const ADDR_IN_USE: &str = "addr_in_use";
56    pub const ADDR_NOT_AVAILABLE: &str = "addr_not_available";
57    pub const NETWORK_DOWN: &str = "network_down";
58    pub const BROKEN_PIPE: &str = "broken_pipe";
59    pub const ALREADY_EXISTS: &str = "already_exists";
60    pub const WOULD_BLOCK: &str = "would_block";
61    pub const NOT_A_DIRECTORY: &str = "not_a_directory";
62    pub const IS_A_DIRECTORY: &str = "is_a_directory";
63    pub const DIRECTORY_NOT_EMPTY: &str = "directory_not_empty";
64    pub const READ_ONLY_FILESYSTEM: &str = "read_only_filesystem";
65    pub const STALE_NETWORK_FILE_HANDLE: &str = "stale_network_file_handle";
66    pub const INVALID_INPUT: &str = "invalid_input";
67    pub const INVALID_DATA: &str = "invalid_data";
68    pub const TIMED_OUT: &str = "timed_out";
69    pub const WRITE_ZERO: &str = "write_zero";
70    pub const STORAGE_FULL: &str = "storage_full";
71    pub const NOT_SEEKABLE: &str = "not_seekable";
72    pub const QUOTA_EXCEEDED: &str = "quota_exceeded";
73    pub const RESOURCE_BUSY: &str = "resource_busy";
74    pub const EXECUTABLE_FILE_BUSY: &str = "executable_file_busy";
75    pub const DEADLOCK: &str = "deadlock";
76    pub const CROSSES_DEVICES: &str = "crosses_devices";
77    pub const TOO_MANY_LINKS: &str = "too_many_links";
78    pub const INVALID_FILENAME: &str = "invalid_filename";
79    pub const ARGUMENT_LIST_TOO_LONG: &str = "argument_list_too_long";
80    pub const INTERRUPTED: &str = "interrupted";
81    pub const UNSUPPORTED: &str = "unsupported";
82    pub const UNEXPECTED_EOF: &str = "unexpected_eof";
83    pub const OUT_OF_MEMORY: &str = "out_of_memory";
84    pub const OTHER: &str = "other";
85
86    /// Creates a new [ErrorKind].
87    pub const fn new() -> Self {
88        Self::Other
89    }
90
91    /// Converts the [ErrorKind] to a [std::io::ErrorKind].
92    pub const fn from_io(err: std::io::ErrorKind) -> Self {
93        match err {
94            std::io::ErrorKind::NotFound => Self::NotFound,
95            std::io::ErrorKind::PermissionDenied => Self::PermissionDenied,
96            std::io::ErrorKind::ConnectionRefused => Self::ConnectionRefused,
97            std::io::ErrorKind::ConnectionReset => Self::ConnectionReset,
98            std::io::ErrorKind::HostUnreachable => Self::HostUnreachable,
99            std::io::ErrorKind::NetworkUnreachable => Self::NetworkUnreachable,
100            std::io::ErrorKind::ConnectionAborted => Self::ConnectionAborted,
101            std::io::ErrorKind::NotConnected => Self::NotConnected,
102            std::io::ErrorKind::AddrInUse => Self::AddrInUse,
103            std::io::ErrorKind::AddrNotAvailable => Self::AddrNotAvailable,
104            std::io::ErrorKind::NetworkDown => Self::NetworkDown,
105            std::io::ErrorKind::BrokenPipe => Self::BrokenPipe,
106            std::io::ErrorKind::AlreadyExists => Self::AlreadyExists,
107            std::io::ErrorKind::WouldBlock => Self::WouldBlock,
108            std::io::ErrorKind::NotADirectory => Self::NotADirectory,
109            std::io::ErrorKind::IsADirectory => Self::IsADirectory,
110            std::io::ErrorKind::DirectoryNotEmpty => Self::DirectoryNotEmpty,
111            std::io::ErrorKind::ReadOnlyFilesystem => Self::ReadOnlyFilesystem,
112            std::io::ErrorKind::StaleNetworkFileHandle => Self::StaleNetworkFileHandle,
113            std::io::ErrorKind::InvalidInput => Self::InvalidInput,
114            std::io::ErrorKind::InvalidData => Self::InvalidData,
115            std::io::ErrorKind::TimedOut => Self::TimedOut,
116            std::io::ErrorKind::WriteZero => Self::WriteZero,
117            std::io::ErrorKind::StorageFull => Self::StorageFull,
118            std::io::ErrorKind::NotSeekable => Self::NotSeekable,
119            std::io::ErrorKind::QuotaExceeded => Self::QuotaExceeded,
120            std::io::ErrorKind::ResourceBusy => Self::ResourceBusy,
121            std::io::ErrorKind::ExecutableFileBusy => Self::ExecutableFileBusy,
122            std::io::ErrorKind::Deadlock => Self::Deadlock,
123            std::io::ErrorKind::CrossesDevices => Self::CrossesDevices,
124            std::io::ErrorKind::TooManyLinks => Self::TooManyLinks,
125            std::io::ErrorKind::InvalidFilename => Self::InvalidFilename,
126            std::io::ErrorKind::ArgumentListTooLong => Self::ArgumentListTooLong,
127            std::io::ErrorKind::Interrupted => Self::Interrupted,
128            std::io::ErrorKind::Unsupported => Self::Unsupported,
129            std::io::ErrorKind::UnexpectedEof => Self::UnexpectedEof,
130            std::io::ErrorKind::OutOfMemory => Self::OutOfMemory,
131            _ => Self::Other,
132        }
133    }
134
135    /// Converts the [ErrorKind] to a [std::io::ErrorKind].
136    pub const fn as_io(&self) -> std::io::ErrorKind {
137        match self {
138            Self::NotFound => std::io::ErrorKind::NotFound,
139            Self::PermissionDenied => std::io::ErrorKind::PermissionDenied,
140            Self::ConnectionRefused => std::io::ErrorKind::ConnectionRefused,
141            Self::ConnectionReset => std::io::ErrorKind::ConnectionReset,
142            Self::HostUnreachable => std::io::ErrorKind::HostUnreachable,
143            Self::NetworkUnreachable => std::io::ErrorKind::NetworkUnreachable,
144            Self::ConnectionAborted => std::io::ErrorKind::ConnectionAborted,
145            Self::NotConnected => std::io::ErrorKind::NotConnected,
146            Self::AddrInUse => std::io::ErrorKind::AddrInUse,
147            Self::AddrNotAvailable => std::io::ErrorKind::AddrNotAvailable,
148            Self::NetworkDown => std::io::ErrorKind::NetworkDown,
149            Self::BrokenPipe => std::io::ErrorKind::BrokenPipe,
150            Self::AlreadyExists => std::io::ErrorKind::AlreadyExists,
151            Self::WouldBlock => std::io::ErrorKind::WouldBlock,
152            Self::NotADirectory => std::io::ErrorKind::NotADirectory,
153            Self::IsADirectory => std::io::ErrorKind::IsADirectory,
154            Self::DirectoryNotEmpty => std::io::ErrorKind::DirectoryNotEmpty,
155            Self::ReadOnlyFilesystem => std::io::ErrorKind::ReadOnlyFilesystem,
156            Self::StaleNetworkFileHandle => std::io::ErrorKind::StaleNetworkFileHandle,
157            Self::InvalidInput => std::io::ErrorKind::InvalidInput,
158            Self::InvalidData => std::io::ErrorKind::InvalidData,
159            Self::TimedOut => std::io::ErrorKind::TimedOut,
160            Self::WriteZero => std::io::ErrorKind::WriteZero,
161            Self::StorageFull => std::io::ErrorKind::StorageFull,
162            Self::NotSeekable => std::io::ErrorKind::NotSeekable,
163            Self::QuotaExceeded => std::io::ErrorKind::QuotaExceeded,
164            Self::ResourceBusy => std::io::ErrorKind::ResourceBusy,
165            Self::ExecutableFileBusy => std::io::ErrorKind::ExecutableFileBusy,
166            Self::Deadlock => std::io::ErrorKind::Deadlock,
167            Self::CrossesDevices => std::io::ErrorKind::CrossesDevices,
168            Self::TooManyLinks => std::io::ErrorKind::TooManyLinks,
169            Self::InvalidFilename => std::io::ErrorKind::InvalidFilename,
170            Self::ArgumentListTooLong => std::io::ErrorKind::ArgumentListTooLong,
171            Self::Interrupted => std::io::ErrorKind::Interrupted,
172            Self::Unsupported => std::io::ErrorKind::Unsupported,
173            Self::UnexpectedEof => std::io::ErrorKind::UnexpectedEof,
174            Self::OutOfMemory => std::io::ErrorKind::OutOfMemory,
175            _ => std::io::ErrorKind::Other,
176        }
177    }
178
179    pub const fn as_str(&self) -> &'static str {
180        match self {
181            Self::NotFound => Self::NOT_FOUND,
182            Self::PermissionDenied => Self::PERMISSION_DENIED,
183            Self::ConnectionRefused => Self::CONNECTION_REFUSED,
184            Self::ConnectionReset => Self::CONNECTION_RESET,
185            Self::HostUnreachable => Self::HOST_UNREACHABLE,
186            Self::NetworkUnreachable => Self::NETWORK_UNREACHABLE,
187            Self::ConnectionAborted => Self::CONNECTION_ABORTED,
188            Self::NotConnected => Self::NOT_CONNECTED,
189            Self::AddrInUse => Self::ADDR_IN_USE,
190            Self::AddrNotAvailable => Self::ADDR_NOT_AVAILABLE,
191            Self::NetworkDown => Self::NETWORK_DOWN,
192            Self::BrokenPipe => Self::BROKEN_PIPE,
193            Self::AlreadyExists => Self::ALREADY_EXISTS,
194            Self::WouldBlock => Self::WOULD_BLOCK,
195            Self::NotADirectory => Self::NOT_A_DIRECTORY,
196            Self::IsADirectory => Self::IS_A_DIRECTORY,
197            Self::DirectoryNotEmpty => Self::DIRECTORY_NOT_EMPTY,
198            Self::ReadOnlyFilesystem => Self::READ_ONLY_FILESYSTEM,
199            Self::StaleNetworkFileHandle => Self::STALE_NETWORK_FILE_HANDLE,
200            Self::InvalidInput => Self::INVALID_INPUT,
201            Self::InvalidData => Self::INVALID_DATA,
202            Self::TimedOut => Self::TIMED_OUT,
203            Self::WriteZero => Self::WRITE_ZERO,
204            Self::StorageFull => Self::STORAGE_FULL,
205            Self::NotSeekable => Self::NOT_SEEKABLE,
206            Self::QuotaExceeded => Self::QUOTA_EXCEEDED,
207            Self::ResourceBusy => Self::RESOURCE_BUSY,
208            Self::ExecutableFileBusy => Self::EXECUTABLE_FILE_BUSY,
209            Self::Deadlock => Self::DEADLOCK,
210            Self::CrossesDevices => Self::CROSSES_DEVICES,
211            Self::TooManyLinks => Self::TOO_MANY_LINKS,
212            Self::InvalidFilename => Self::INVALID_FILENAME,
213            Self::ArgumentListTooLong => Self::ARGUMENT_LIST_TOO_LONG,
214            Self::Interrupted => Self::INTERRUPTED,
215            Self::Unsupported => Self::UNSUPPORTED,
216            Self::UnexpectedEof => Self::UNEXPECTED_EOF,
217            Self::OutOfMemory => Self::OUT_OF_MEMORY,
218            Self::Other => Self::OTHER,
219        }
220    }
221}
222
223impl Default for ErrorKind {
224    fn default() -> Self {
225        Self::new()
226    }
227}
228
229impl From<std::io::ErrorKind> for ErrorKind {
230    fn from(err: std::io::ErrorKind) -> Self {
231        Self::from_io(err)
232    }
233}
234
235impl From<ErrorKind> for std::io::ErrorKind {
236    fn from(err: ErrorKind) -> Self {
237        err.as_io()
238    }
239}
240
241impl std::fmt::Display for ErrorKind {
242    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
243        write!(f, "{}", self.as_str())
244    }
245}