error_mapper/errors/
the_error.rs1use std::fmt::{Display, Formatter};
2use chrono::{NaiveDate, NaiveTime};
3use crate::SystemErrorCodes;
4
5pub type TheResult<T> = Result<T, TheError>;
8
9#[derive(Debug, Default)]
13pub struct TheError {
14 pub error: TheErrorType,
15 pub file: Option<String>,
16 pub location: Option<(u32, u32)>,
17 pub datestamp: Option<NaiveDate>,
18 pub timestamp: Option<NaiveTime>
19}
20
21#[derive(Debug, Default)]
24pub struct TheErrorType {
25 pub error_type: SystemErrorCodes,
26 pub error_content: String,
27}
28
29impl TheError {
30 pub fn new(
33 error_type: SystemErrorCodes,
34 error_content: String
35 ) -> Self {
36 Self {
37 error: TheErrorType {
38 error_type,
39 error_content
40 },
41 file: None,
42 location: None,
43 datestamp: None,
44 timestamp: None
45 }
46 }
47
48 pub fn get_type(&self) -> &SystemErrorCodes {
50 &self.error.error_type
51 }
52
53 pub fn get_content(&self) -> &String {
55 &self.error.error_content
56 }
57
58 pub fn get_location_info(&self) -> &Option<(u32, u32)> {
60 &self.location
61 }
62
63 pub fn get_datestamp(&self) -> &Option<NaiveDate> {
65 &self.datestamp
66 }
67
68 pub fn get_timestamp(&self) -> &Option<NaiveTime> {
70 &self.timestamp
71 }
72
73 pub fn get_datetime(&self) -> Option<(String, String)> {
75 let date = if let Some(datestamp) = self.datestamp {
77 datestamp.to_string()
78 } else {
79 "".to_string()
80 };
81 let time = if let Some(timestamp) = self.timestamp {
82 timestamp.to_string()
83 } else {
84 "".to_string()
85 };
86
87 Some((date, time))
88 }
89
90 pub fn with_type(mut self, error_type: SystemErrorCodes) -> Self {
92 self.error.error_type = error_type;
93 self
94 }
95
96 pub fn with_content(mut self, error_content: String) -> Self {
98 self.error.error_content = error_content;
99 self
100 }
101
102 pub fn with_file_data(mut self, file: String) -> Self {
104 self.file = Some(file);
105 self
106 }
107
108 pub fn with_location_data(mut self, location: (u32, u32)) -> Self {
110 self.location = Some(location);
111 self
112 }
113
114 pub fn with_datestamp_data(mut self, datestamp: NaiveDate) -> Self {
116 self.datestamp = Some(datestamp);
117 self
118 }
119
120 pub fn with_timestamp_data(mut self, timestamp: NaiveTime) -> Self {
122 self.timestamp = Some(timestamp);
123 self
124 }
125
126 pub fn add_error_type(&mut self, error_type: SystemErrorCodes) {
128 self.error.error_type = error_type;
129 }
130
131 pub fn add_error_content(&mut self, content: String) {
133 self.error.error_content = content;
134 }
135
136 pub fn add_file_data(&mut self, file: String) {
138 self.file = Some(file);
139 }
140
141 pub fn add_location_data(&mut self, location: (u32, u32)) {
143 self.location = Some(location);
144 }
145
146 pub fn add_datestamp_data(&mut self, datestamp: NaiveDate) {
148 self.datestamp = Some(datestamp);
149 }
150
151 pub fn add_timestamp_data(&mut self, timestamp: NaiveTime) {
153 self.timestamp = Some(timestamp);
154 }
155}
156
157impl Display for TheError {
158 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
163
164 if self.file.is_some() && self.location.is_some() {
165 write!(f, "{}: {} @{}: {}|{}", self.error.error_type, self.error.error_content, self.file.clone().unwrap(), self.location.unwrap().0, self.location.unwrap().1)
166 .expect("Couldn't display message!!");
167 } else {
168 write!(f, "{}: {}", self.error.error_type, self.error.error_content)
169 .expect("Couldn't display message!!");
170 }
171
172 Ok(())
173 }
174}
175
176impl From<TheError> for TheErrorType {
177 fn from(value: TheError) -> Self {
178 Self {
179 error_type: value.error.error_type,
180 error_content: value.error.error_content
181 }
182 }
183}