api_response/error_code/
mod.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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
mod errpath;
mod errtype;
pub mod ety_grpc;

use std::{
    fmt::Display,
    ops::{Add, BitOr},
    thread::LocalKey,
};

pub use errpath::*;
pub use errtype::*;
use getset2::Getset2;

use crate::ApiError;

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub struct ErrDecl {
    pub err_type: ErrType,
    pub err_path: ErrPath,
}

impl ErrDecl {
    #[inline]
    pub const fn new(err_type: ErrType, err_path: ErrPath) -> Self {
        Self { err_type, err_path }
    }
    #[inline]
    pub const fn err_flag(&self) -> i32 {
        self.err_type.flag() as i32
    }
    #[inline]
    pub const fn err_path_flag(&self) -> i32 {
        self.err_path.path_flag()
    }
    #[inline]
    pub const fn text(&self) -> &'static str {
        self.err_type.text()
    }
    pub const fn err_type(&self) -> &ErrType {
        &self.err_type
    }
    pub const fn err_path(&self) -> &ErrPath {
        &self.err_path
    }
    #[inline(always)]
    pub const fn extract(&self) -> ErrBrief {
        ErrBrief::new(self.err_type, &self.err_path)
    }
    #[inline(always)]
    pub fn api_error(&self) -> ApiError {
        self.extract().api_error()
    }
}

impl Display for ErrDecl {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}, {}", self.extract(), self.err_path)
    }
}

#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Getset2)]
#[getset2(get_copy(pub, const))]
#[non_exhaustive]
pub struct ErrBrief {
    message: &'static str,
    code: i32,
}
impl Display for ErrBrief {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{} ErrCode({})", self.message, self.code)
    }
}
impl ErrBrief {
    #[inline(always)]
    pub const fn new(err_type: ErrType, err_path: &ErrPath) -> Self {
        Self {
            message: err_type.text(),
            code: (err_type.flag() as i32 * 1000000) + err_path.path_flag(),
        }
    }
    #[inline(always)]
    pub fn api_error(&self) -> ApiError {
        ApiError {
            code: self.code,
            message: self.message.to_owned(),
            details: None,
            source: None,
        }
    }
}

impl BitOr<&ErrPath> for ErrType {
    type Output = ErrBrief;

    #[inline]
    fn bitor(self, rhs: &ErrPath) -> Self::Output {
        self.extract(rhs)
    }
}
impl BitOr<&'static LocalKey<ErrPath>> for ErrType {
    type Output = ErrBrief;

    #[inline]
    fn bitor(self, rhs: &'static LocalKey<ErrPath>) -> Self::Output {
        rhs.with(|v| self.extract(v))
    }
}
impl Add<&ErrPath> for ErrType {
    type Output = ApiError;

    #[inline]
    fn add(self, rhs: &ErrPath) -> Self::Output {
        self.api_error(rhs)
    }
}
impl Add<&'static LocalKey<ErrPath>> for ErrType {
    type Output = ApiError;

    #[inline]
    fn add(self, rhs: &'static LocalKey<ErrPath>) -> Self::Output {
        rhs.with(|v| self.api_error(v))
    }
}

#[cfg(test)]
mod tests {
    // use std::cell::LazyCell;

    // use super::{ErrDecl, ErrPath, ErrFlag, ErrType, ModSection,
    // ModSegment}; use crate::ApiError;

    // #[test]
    // fn display() {
    //     const ET: ErrType = ErrType::new(ErrFlag::E100, "The operation was
    // cancelled.");     const MS0: ModSection =
    // ModSection::new(ModSegment::M00, "module 0");     const MS1:
    // ModSection = ModSection::new(ModSegment::M01, "module 01");     const
    // MS2: ModSection = ModSection::new(ModSegment::M02, "module 012");
    //     const MP: ErrPath = ErrPath::new(MS0, MS1, MS2);
    //     const EC: ErrDecl = ErrDecl::new(ET, MP);
    //     assert_eq!(
    //         "The operation was cancelled. ErrDecl(100000102), M00(module
    // 0)/M01(module 01)/M02(module 012)",         EC.to_string()
    //     );

    //     let err_code: ErrDecl = ET | MP;
    //     assert_eq!(EC, err_code);
    //     let api_error: ApiError = ET + MP;
    //     assert_eq!(EC.to_api_error().code(), api_error.code());
    //     let mp: LazyCell<ErrPath> = LazyCell::new(|| MP);
    //     let err_code: ErrDecl = ET | *mp;
    //     assert_eq!(EC, err_code);
    //     let api_error: ApiError = ET + *mp;
    //     assert_eq!(EC.to_api_error().code(), api_error.code());
    // }
}