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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#![no_std]
use proc_macro_hack::proc_macro_hack;

#[proc_macro_hack(fake_call_site)]
pub use no_error_macro::error_message;

pub type ErrorCode = u32;
pub type ErrorMessage = (&'static str, &'static str);

pub enum NoError {
    Code(ErrorCode),
    Message((&'static str, &'static str)),
}

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

pub trait Error {
    fn code(&self) -> Option<ErrorCode>;
    fn description(&self) -> &'static str;
    fn source(&self) -> &'static str;
    fn cstr_description(&self) -> *const u8;
    fn cstr_source(&self) -> *const u8;
}

const GENERIC_ERROR_CODE: &'static str = "ERR_CODE\0";
const EMPTY: &'static str = "\0";

impl Error for NoError {
    #[inline]
    fn code(&self) -> Option<ErrorCode> {
        match self {
            NoError::Code(c) => Some(*c),
            NoError::Message(_) => None,
        }
    }

    #[inline]
    fn description(&self) -> &'static str {
        match self {
            NoError::Code(_) => &GENERIC_ERROR_CODE[..GENERIC_ERROR_CODE.len() - 1],
            NoError::Message(m) => &m.0[..m.0.len() - 1],
        }
    }

    #[inline]
    fn cstr_description(&self) -> *const u8 {
        match self {
            NoError::Code(_) => GENERIC_ERROR_CODE.as_ptr(),
            NoError::Message(m) => m.0.as_ptr(),
        }
    }

    #[inline]
    fn source(&self) -> &'static str {
        match self {
            NoError::Code(_) => &EMPTY[..EMPTY.len() - 1],
            NoError::Message(m) => &m.1[..m.1.len() - 1],
        }
    }

    #[inline]
    fn cstr_source(&self) -> *const u8 {
        match self {
            NoError::Code(_) => EMPTY.as_ptr(),
            NoError::Message(m) => m.1.as_ptr(),
        }
    }
}

#[macro_export]
macro_rules! error_code {
    ( $x:expr ) => {{
        Err(no_error::NoError::Code($x))
    }};
}