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
76
77
78
79
80
81
82
83
84
85
86
use std::error::Error as StdError;
use std::ffi::CStr;
use std::fmt;
use std::result::Result as StdResult;
use libc::c_char;
#[derive(thiserror::Error, Eq, PartialEq)]
#[repr(C)]
pub struct Error {
r#type: ErrorType,
msg: *mut c_char,
}
#[allow(dead_code, non_camel_case_types)]
#[derive(Eq, PartialEq)]
#[repr(C)]
enum ErrorType {
None = -1,
Exception,
Validation,
}
impl Error {
pub const fn new() -> Self {
Self { r#type: ErrorType::None, msg: std::ptr::null_mut() }
}
}
impl Default for Error {
#[inline]
fn default() -> Self {
Self::new()
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if !self.msg.is_null() {
fmt::Debug::fmt(unsafe { CStr::from_ptr(self.msg) }, f)
} else {
use ErrorType::*;
let msg = match self.r#type {
None => return Ok(()),
Exception => "exception",
Validation => "validation",
};
write!(f, "{}", msg)
}
}
}
impl Error {
#[inline]
pub fn into_err_or_else<Ok, Err, F>(self, f: F) -> StdResult<Ok, Err>
where
Err: StdError + From<self::Error>,
F: FnOnce() -> Ok,
{
(!self.is_err()).then(f).ok_or_else(|| self.into())
}
#[inline]
pub fn into_err_or_flatten<Ok, Err, F>(self, f: F) -> StdResult<Ok, Err>
where
Err: StdError + From<self::Error>,
F: FnOnce() -> StdResult<Ok, Err>,
{
self.into_err_or_else(f)?
}
#[inline]
pub const fn is_err(&self) -> bool {
!matches!(self.r#type, ErrorType::None)
}
}