tiny_std/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use core::fmt::{Debug, Display, Formatter};

use rusl::error::Errno;

pub type Result<T> = core::result::Result<T, Error>;

#[derive(Copy, Clone)]
pub enum Error {
    Uncategorized(&'static str),
    Os { msg: &'static str, code: Errno },
}

impl Error {
    #[inline]
    pub(crate) const fn no_code(msg: &'static str) -> Self {
        Self::Uncategorized(msg)
    }

    #[inline]
    pub(crate) const fn os(msg: &'static str, code: Errno) -> Self {
        Self::Os { msg, code }
    }

    #[inline]
    #[must_use]
    pub fn matches_errno(&self, errno: Errno) -> bool {
        if let Self::Os { code, .. } = self {
            *code == errno
        } else {
            false
        }
    }
}

impl From<rusl::Error> for Error {
    fn from(e: rusl::Error) -> Self {
        if let Some(code) = e.code {
            Self::Os { msg: e.msg, code }
        } else {
            Self::Uncategorized(e.msg)
        }
    }
}

impl Debug for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        Display::fmt(self, f)
    }
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        match self {
            Error::Uncategorized(msg) => f.write_fmt(format_args!("Error: {msg}")),
            Error::Os { msg, code } => {
                f.write_fmt(format_args!("OsError {{ msg: `{msg}`, code: {code} }}"))
            }
        }
    }
}