1use core::fmt::{Debug, Display, Formatter};
2
3use rusl::error::Errno;
4
5pub type Result<T> = core::result::Result<T, Error>;
6
7#[derive(Copy, Clone)]
8pub enum Error {
9 Uncategorized(&'static str),
10 Timeout,
11 Os { msg: &'static str, code: Errno },
12}
13
14impl Error {
15 #[inline]
16 pub(crate) const fn no_code(msg: &'static str) -> Self {
17 Self::Uncategorized(msg)
18 }
19
20 #[inline]
21 pub(crate) const fn os(msg: &'static str, code: Errno) -> Self {
22 Self::Os { msg, code }
23 }
24
25 #[inline]
26 #[must_use]
27 pub fn matches_errno(&self, errno: Errno) -> bool {
28 if let Self::Os { code, .. } = self {
29 *code == errno
30 } else {
31 false
32 }
33 }
34}
35
36impl From<rusl::Error> for Error {
37 fn from(e: rusl::Error) -> Self {
38 if let Some(code) = e.code {
39 Self::Os { msg: e.msg, code }
40 } else {
41 Self::Uncategorized(e.msg)
42 }
43 }
44}
45
46impl Debug for Error {
47 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
48 Display::fmt(self, f)
49 }
50}
51
52impl Display for Error {
53 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
54 match self {
55 Error::Uncategorized(msg) => f.write_fmt(format_args!("Error: {msg}")),
56 Error::Os { msg, code } => {
57 f.write_fmt(format_args!("OsError {{ msg: `{msg}`, code: {code} }}"))
58 }
59 Error::Timeout => f.write_fmt(format_args!("Error: Timeout")),
60 }
61 }
62}