except_plugin/core/
builder.rs

1//! # builders for exceptions
2//! ```txt
3//! @author:syf20020816@Outlook.com
4//! @date:2023/8/13
5//! @version:0.0.1
6//! @description:
7//! ```
8
9use std::collections::HashMap;
10use std::ops::Deref;
11use std::path::PathBuf;
12use std::time::{SystemTime, Duration, UNIX_EPOCH};
13use crate::{builder_impl, common_param_impl, target_param_impl, out_of_bounds_impl, reason_param_impl, sql_param_impl};
14use crate::core::{CommonParamImpl, NullPointerException, TargetParamImpl, UNSUPPORTED_OPERATION_MSG};
15use super::{
16    SuperException, Exceptions, ExceptionLevel, SUPER_MSG, EASY_MSG, NULL_POINTER_MSG, OUT_OF_BOUNDS_MSG,
17    ExceptionCode, FromBuilder, SuperBuilderImpl, EasyException, Reasons, UnSupportedOpException,
18    ArrayIndexOutOfBoundsException, OutOfBoundsParamImpl, ReasonParamImpl, UnSupportedReasons,
19    SQLReasons, SQLParamImpl, SQL_MSG, SQLException,
20};
21
22/// # 异常工厂
23///
24pub struct ExceptionFactory;
25
26impl ExceptionFactory {
27    pub fn new<E, B>() -> B where B: SuperBuilderImpl<E> {
28        B::new()
29    }
30}
31
32/// # Supper Builder for Supper Exception
33/// use super_pattern
34#[derive(Clone, Debug, PartialEq)]
35pub struct SuperBuilder {
36    code: u32,
37    msg: String,
38    level: ExceptionLevel,
39    timestamp: Duration,
40    e_type: Exceptions,
41}
42
43builder_impl!(SuperBuilder,SuperException);
44
45impl Default for SuperBuilder {
46    fn default() -> Self {
47        SuperBuilder {
48            code: ExceptionCode::SUPER,
49            msg: String::from(SUPER_MSG),
50            level: ExceptionLevel::Info,
51            timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
52            e_type: Exceptions::Super,
53        }
54    }
55}
56
57//----------------------------------------------------------
58#[derive(Clone, Debug, PartialEq)]
59pub struct EasyExceptionBuilder {
60    code: u32,
61    msg: String,
62    level: ExceptionLevel,
63    line: u32,
64    path: PathBuf,
65    timestamp: Duration,
66    e_type: Exceptions,
67}
68
69impl Default for EasyExceptionBuilder {
70    fn default() -> Self {
71        EasyExceptionBuilder {
72            code: ExceptionCode::COMMON,
73            msg: String::from(EASY_MSG),
74            level: ExceptionLevel::Info,
75            line: 0,
76            path: PathBuf::new(),
77            timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
78            e_type: Exceptions::Easy,
79        }
80    }
81}
82
83builder_impl!(EasyExceptionBuilder,EasyException);
84
85common_param_impl!(EasyExceptionBuilder);
86
87//----------------------------------------------------------------
88#[derive(Clone, Debug, PartialEq)]
89pub struct NullPointerExceptionBuilder {
90    code: u32,
91    msg: String,
92    level: ExceptionLevel,
93    timestamp: Duration,
94    line: u32,
95    path: PathBuf,
96    target: Option<String>,
97    e_type: Exceptions,
98}
99
100impl Default for NullPointerExceptionBuilder {
101    fn default() -> Self {
102        NullPointerExceptionBuilder {
103            code: ExceptionCode::COMMON,
104            msg: String::from(NULL_POINTER_MSG),
105            level: ExceptionLevel::Info,
106            timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
107            line: 0,
108            path: PathBuf::new(),
109            target: None,
110            e_type: Exceptions::Easy,
111        }
112    }
113}
114
115builder_impl!(NullPointerExceptionBuilder,NullPointerException);
116
117common_param_impl!(NullPointerExceptionBuilder);
118
119target_param_impl!(NullPointerExceptionBuilder);
120
121//----------------------------------------------------------------
122#[derive(Clone, Debug, PartialEq)]
123pub struct ArrayIndexOutOfBoundsBuilder {
124    code: u32,
125    msg: String,
126    level: ExceptionLevel,
127    timestamp: Duration,
128    line: u32,
129    path: PathBuf,
130    target: Option<String>,
131    len: usize,
132    index: usize,
133    e_type: Exceptions,
134}
135
136impl Default for ArrayIndexOutOfBoundsBuilder {
137    fn default() -> Self {
138        ArrayIndexOutOfBoundsBuilder {
139            code: ExceptionCode::ARRAY_INDEX_OUT_OF,
140            msg: String::from(OUT_OF_BOUNDS_MSG),
141            level: ExceptionLevel::Info,
142            timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
143            line: 0,
144            path: PathBuf::new(),
145            target: None,
146            len: 0,
147            index: 0,
148            e_type: Exceptions::ArrayIndexOutOfBounds,
149        }
150    }
151}
152
153builder_impl!(ArrayIndexOutOfBoundsBuilder,ArrayIndexOutOfBoundsException);
154
155common_param_impl!(ArrayIndexOutOfBoundsBuilder);
156
157target_param_impl!(ArrayIndexOutOfBoundsBuilder);
158
159out_of_bounds_impl!(ArrayIndexOutOfBoundsBuilder);
160
161//----------------------------------------------------------------
162#[derive(Clone, Debug, PartialEq)]
163pub struct UnSupportedOpExceptionBuilder {
164    code: u32,
165    msg: String,
166    line: u32,
167    path: PathBuf,
168    level: ExceptionLevel,
169    reason: Reasons,
170    e_type: Exceptions,
171    timestamp: Duration,
172}
173
174impl Default for UnSupportedOpExceptionBuilder {
175    fn default() -> Self {
176        UnSupportedOpExceptionBuilder {
177            code: ExceptionCode::UNSUPPORTED_OPERATION,
178            msg: String::from(UNSUPPORTED_OPERATION_MSG),
179            level: ExceptionLevel::Info,
180            reason: Reasons::UnSupported(UnSupportedReasons::Other),
181            e_type: Exceptions::UnSupportedOperation,
182            timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
183            line: 0,
184            path: PathBuf::new(),
185        }
186    }
187}
188
189builder_impl!(UnSupportedOpExceptionBuilder,UnSupportedOpException);
190
191common_param_impl!(UnSupportedOpExceptionBuilder);
192
193reason_param_impl!(UnSupportedOpExceptionBuilder);
194
195//----------------------------------------------------------------
196#[derive(Clone, Debug, PartialEq)]
197pub struct SQLExceptionBuilder {
198    code: u32,
199    msg: String,
200    line: u32,
201    path: PathBuf,
202    level: ExceptionLevel,
203    reason: Reasons,
204    stmt: Option<String>,
205    tips: HashMap<String, String>,
206    timestamp: Duration,
207    e_type: Exceptions,
208}
209
210impl Default for SQLExceptionBuilder {
211    fn default() -> Self {
212        SQLExceptionBuilder {
213            code: ExceptionCode::SQL,
214            msg: String::from(SQL_MSG),
215            level: ExceptionLevel::Info,
216            reason: Reasons::SQL(SQLReasons::Query),
217            stmt: None,
218            e_type: Exceptions::SQL,
219            timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
220            line: 0,
221            path: PathBuf::new(),
222            tips: HashMap::new(),
223        }
224    }
225}
226
227builder_impl!(SQLExceptionBuilder,SQLException);
228
229common_param_impl!(SQLExceptionBuilder);
230
231reason_param_impl!(SQLExceptionBuilder);
232
233sql_param_impl!(SQLExceptionBuilder);