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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
mod errpath;
mod errtype;
pub mod ety_grpc;
pub mod tally;

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

pub use errpath::*;
pub use errtype::*;
use getset2::Getset2;
use serde::{Deserialize, Serialize};

use crate::ApiError;

#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize)]
#[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, Serialize, Deserialize)]
#[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<&'static str> for ErrType {
    type Output = ErrType;

    #[inline(always)]
    fn bitor(self, rhs: &'static str) -> Self::Output {
        self.with_text(rhs)
    }
}

impl Add<ErrPath> for ErrType {
    type Output = ErrDecl;

    #[inline(always)]
    fn add(self, rhs: ErrPath) -> Self::Output {
        self.declare(rhs)
    }
}
impl Add<&ErrPath> for ErrType {
    type Output = ErrDecl;

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

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

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

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

    #[inline(always)]
    fn bitor(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, ErrFlag, ErrPath, ErrPathParent, ErrPathRoot, ErrType, X00};
    use crate::ApiError;

    #[test]
    fn display() {
        const ET: ErrType = ErrType::new(ErrFlag::E100, "The operation was cancelled.");
        const EP_LV1: ErrPathRoot = X00("product");
        const EP_LV2: ErrPathParent = EP_LV1.Y01("system");
        const EP_LV3: ErrPath = EP_LV2.Z20("module");
        const EC: ErrDecl = ErrDecl::new(ET, EP_LV3);
        assert_eq!(
            "The operation was cancelled. ErrCode(100000120), X00(product)/Y01(system)/Z20(module)",
            EC.to_string()
        );

        let api_error: ApiError = ET | &EP_LV3;
        assert_eq!(EC.api_error().code(), api_error.code());
        let mp: LazyCell<ErrPath> = LazyCell::new(|| EP_LV3);
        let api_error: ApiError = ET | &*mp;
        assert_eq!(EC.api_error().code(), api_error.code());
    }
}