error_mapper/errors/
the_error.rs

1use std::fmt::{Display, Formatter};
2use chrono::{NaiveDate, NaiveTime};
3use crate::SystemErrorCodes;
4
5/// Alias to a Result<T, TheError> type, with TheError being a struct that contains extra details
6/// of the original error
7pub type TheResult<T> = Result<T, TheError>;
8
9/// Struct that contains the **error** itself mapped within a **TheErrorType** struct, with its **error
10/// content** configured in the origin crate, the **file**, **location**, **datestamp** and
11/// **timestamp** data of when the error was remapped using the map_to_new_error! macro.
12#[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/// Smaller error struct to contain the **mapped error type** as a **SystemErrorCodes** enum
22/// and the **error content** from the origin error
23#[derive(Debug, Default)]
24pub struct TheErrorType {
25    pub error_type: SystemErrorCodes,
26    pub error_content: String,
27}
28
29impl TheError {
30    /// **Description**: Creates a new error of TheError type, receiving as parameters the error
31    /// type and content, and capturing the file, location, datestamp and timestamp info
32    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    /// **Returns** the SystemErrorCode associated with this error
49    pub fn get_type(&self) -> &SystemErrorCodes {
50        &self.error.error_type
51    }
52
53    /// **Returns** the error content mapped from the origin error
54    pub fn get_content(&self) -> &String {
55        &self.error.error_content
56    }
57
58    /// **Returns** the location String in file-line-column format
59    pub fn get_location_info(&self) -> &Option<(u32, u32)> {
60        &self.location
61    }
62
63    /// **Returns** the error's NaiveDate datestamp as a String
64    pub fn get_datestamp(&self) -> &Option<NaiveDate> {
65        &self.datestamp
66    }
67
68    /// **Returns** the error's NaiveTime timestamp as a String
69    pub fn get_timestamp(&self) -> &Option<NaiveTime> {
70        &self.timestamp
71    }
72
73    /// **Returns** the error's datetime stamp as a String
74    pub fn get_datetime(&self) -> Option<(String, String)> {
75        // format!("{} {}", &self.datestamp, &self.timestamp).to_string()
76        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    /// **Description**: Adds the error type to the error
91    pub fn with_type(mut self, error_type: SystemErrorCodes) -> Self {
92        self.error.error_type = error_type;
93        self
94    }
95
96    /// **Description**: Adds the error content to the error
97    pub fn with_content(mut self, error_content: String) -> Self {
98        self.error.error_content = error_content;
99        self
100    }
101
102    /// **Description**: Adds the file data to the error
103    pub fn with_file_data(mut self, file: String) -> Self {
104        self.file = Some(file);
105        self
106    }
107
108    /// **Description**: Adds the location data to the error
109    pub fn with_location_data(mut self, location: (u32, u32)) -> Self {
110        self.location = Some(location);
111        self
112    }
113
114    /// **Description**: Adds the datestamp data to the error
115    pub fn with_datestamp_data(mut self, datestamp: NaiveDate) -> Self {
116        self.datestamp = Some(datestamp);
117        self
118    }
119
120    /// **Description**: Adds the timestamp data to the error
121    pub fn with_timestamp_data(mut self, timestamp: NaiveTime) -> Self {
122        self.timestamp = Some(timestamp);
123        self
124    }
125
126    /// **Description**: Allows the user to add the error type after creation
127    pub fn add_error_type(&mut self, error_type: SystemErrorCodes) {
128        self.error.error_type = error_type;
129    }
130
131    /// **Description**: Allows the user to add the error content after creation
132    pub fn add_error_content(&mut self, content: String) {
133        self.error.error_content = content;
134    }
135
136    /// **Description**: Allows the user to add the file data after creation
137    pub fn add_file_data(&mut self, file: String) {
138        self.file = Some(file);
139    }
140
141    /// **Description**: Allows the user to add the location data after creation
142    pub fn add_location_data(&mut self, location: (u32, u32)) {
143        self.location = Some(location);
144    }
145
146    /// **Description**: Allows the user to add the datestamp data after creation
147    pub fn add_datestamp_data(&mut self, datestamp: NaiveDate) {
148        self.datestamp = Some(datestamp);
149    }
150
151    /// **Description**: Allows the user to add the timestamp data after creation
152    pub fn add_timestamp_data(&mut self, timestamp: NaiveTime) {
153        self.timestamp = Some(timestamp);
154    }
155}
156
157impl Display for TheError {
158    /// Formatter function to **display** the error in a simple manner.
159    ///
160    /// No overhead, just the error type and content. If the user needs more information and was
161    /// previously stored, it'll be inside TheError struct.
162    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}