api_response/error_code/
tally.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::{
    collections::{BTreeMap, HashSet},
    thread::LocalKey,
};

// re-export
pub use inventory;

use super::{ErrDecl, ErrPath, ErrPathParent, ErrPathRoot, ErrType};

/// Quickly create an `ApiError` builder `ApiErr` and collect error code mode
/// information.
#[macro_export]
macro_rules! api_err {
    ($err_decl:expr) => {{
        $crate::error_code::tally::inventory::submit! {
            $err_decl
        }
        $err_decl.api_error()
    }};
    ($err_type:expr, & $local_key_err_path:expr) => {{
        $crate::error_code::tally::inventory::submit! {
            $crate::error_code::tally::LocalKeyErrDecl::new($err_type, &$local_key_err_path)
        }
        $err_type | &$local_key_err_path
    }};
    ($err_type:expr, $new_text:expr, & $local_key_err_path:expr) => {{
        $crate::error_code::tally::inventory::submit! {
            $crate::error_code::tally::LocalKeyErrDecl::new($err_type.with_text($new_text), &$local_key_err_path)
        }
        ($err_type | $new_text) | &$local_key_err_path
    }};

    ($err_type:expr, $err_path:expr) => {{
        $crate::error_code::tally::inventory::submit! {
            $err_type.declare($err_path)
        }
        $err_type | &$err_path
    }};
    ($err_type:expr, $new_text:expr, $err_path:expr) => {{
        $crate::error_code::tally::inventory::submit! {
            $err_type.with_text($new_text).declare($err_path)
        }
        ($err_type | $new_text) | &$err_path
    }};
}

#[non_exhaustive]
pub struct LocalKeyErrDecl {
    err_type: ErrType,
    err_path: &'static LocalKey<ErrPath>,
}
impl LocalKeyErrDecl {
    pub const fn new(err_type: ErrType, err_path: &'static LocalKey<ErrPath>) -> Self {
        Self { err_type, err_path }
    }
}

inventory::collect!(ErrDecl);
inventory::collect!(LocalKeyErrDecl);

/// Obtain the list of error code declaration.
pub fn tally_err_decl() -> ErrDeclTally {
    let total = inventory::iter::<ErrDecl>
        .into_iter()
        .map(ToOwned::to_owned)
        .chain(
            inventory::iter::<LocalKeyErrDecl>
                .into_iter()
                .map(|v| v.err_type + v.err_path),
        )
        .collect();
    ErrDeclTally { total }
}

#[derive(Debug)]
#[non_exhaustive]
pub struct ErrDeclTally {
    total: Vec<ErrDecl>,
}

pub type ErrDeclTree =
    BTreeMap<ErrPathRoot, BTreeMap<ErrPathParent, BTreeMap<ErrPath, BTreeMap<ErrType, HashSet<ErrDecl>>>>>;

pub type ErrDeclTreeText = BTreeMap<String, BTreeMap<String, BTreeMap<String, BTreeMap<String, HashSet<String>>>>>;

impl ErrDeclTally {
    pub const fn total(&self) -> &Vec<ErrDecl> {
        &self.total
    }
    pub fn unique(&self) -> Vec<ErrDecl> {
        let mut seen = HashSet::new();
        let mut unique = self.total.clone();
        unique.retain(|v| seen.insert(v.to_string()));
        unique
    }
    pub fn tree(&self) -> ErrDeclTree {
        let mut b_tree_map: ErrDeclTree = BTreeMap::new();
        let unique = self.unique();
        for ele in unique {
            let curr = ele.err_path();
            let parent = curr.parent();
            let root = parent.root();
            let typ = ele.err_type();
            b_tree_map
                .entry(root)
                .or_default()
                .entry(parent)
                .or_default()
                .entry(*curr)
                .or_default()
                .entry(*typ)
                .or_default()
                .insert(ele);
        }
        b_tree_map
    }
    pub fn text_tree(&self) -> ErrDeclTreeText {
        let mut b_tree_map: ErrDeclTreeText = BTreeMap::new();
        let unique = self.unique();
        for ele in unique {
            let curr = ele.err_path();
            let parent = curr.parent();
            let root = parent.root();
            let brief = ele.extract();
            b_tree_map
                .entry(format!("X{:02}({})", root.flag(), root.name()))
                .or_default()
                .entry(format!("Y{:02}({})", parent.flag(), parent.name()))
                .or_default()
                .entry(format!("Z{:02}({})", curr.flag(), curr.name()))
                .or_default()
                .entry(format!("ErrCode({})", brief.code()))
                .or_default()
                .insert(brief.message().to_owned());
        }
        b_tree_map
    }
    pub fn json(&self) -> String {
        unsafe { serde_json::to_string_pretty(&self.text_tree()).unwrap_unchecked() }
    }
    pub fn yaml(&self) -> String {
        unsafe { serde_yaml::to_string(&self.text_tree()).unwrap_unchecked() }
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        ApiError,
        error_code::{
            ErrDecl, ErrFlag, ErrPath, ErrPathParent, ErrPathRoot, ErrType, X00, X01,
            tally::{ErrDeclTally, tally_err_decl},
        },
    };

    #[test]
    fn macro_api_err() {
        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);

        let ae0: ApiError = api_err!(EC);
        assert_eq!("The operation was cancelled. Code(100000120)", ae0.to_string());
        let ae1: ApiError = api_err!(ET, EP_LV3);
        let _ = api_err!(ET, EP_LV3);
        assert_eq!("The operation was cancelled. Code(100000120)", ae1.to_string());
        let ae2: ApiError = api_err!(ET, "This is new message.", EP_LV3);
        assert_eq!("This is new message. Code(100000120)", ae2.to_string());

        thread_local! {static EP_LV3_1:ErrPath=X01("product-2").Y01("system-2").Z02("module-2")}
        let ae3: ApiError = api_err!(ET, &EP_LV3_1);
        assert_eq!("The operation was cancelled. Code(100010102)", ae3.to_string());
        let ae4: ApiError = api_err!(ET, "This is new message-2.", &EP_LV3_1);
        assert_eq!("This is new message-2. Code(100010102)", ae4.to_string());

        let s = format!("{:?}", tally_err_decl());
        println!("{s}");

        let tally: ErrDeclTally = tally_err_decl();
        for err_decl in tally.unique() {
            println!("{err_decl}");
        }
        assert_eq!(tally.unique().len(), 4);
        assert_eq!(tally.total().len(), 6);
        println!("{}", tally.json());
        println!("{}", tally.yaml());
    }
}