use std::fmt::{Display, Formatter};
use chrono::{NaiveDate, NaiveTime};
use crate::SystemErrorCodes;
pub type TheResult<T> = Result<T, TheError>;
#[derive(Debug, Default)]
pub struct TheError {
pub error: TheErrorType,
pub file: Option<String>,
pub location: Option<(u32, u32)>,
pub datestamp: Option<NaiveDate>,
pub timestamp: Option<NaiveTime>
}
#[derive(Debug, Default)]
pub struct TheErrorType {
pub error_type: SystemErrorCodes,
pub error_content: String,
}
impl TheError {
pub fn new(
error_type: SystemErrorCodes,
error_content: String
) -> Self {
Self {
error: TheErrorType {
error_type,
error_content
},
file: None,
location: None,
datestamp: None,
timestamp: None
}
}
pub fn get_type(&self) -> &SystemErrorCodes {
&self.error.error_type
}
pub fn get_content(&self) -> &String {
&self.error.error_content
}
pub fn get_location_info(&self) -> &Option<(u32, u32)> {
&self.location
}
pub fn get_datestamp(&self) -> &Option<NaiveDate> {
&self.datestamp
}
pub fn get_timestamp(&self) -> &Option<NaiveTime> {
&self.timestamp
}
pub fn get_datetime(&self) -> Option<(String, String)> {
let date = if let Some(datestamp) = self.datestamp {
datestamp.to_string()
} else {
"".to_string()
};
let time = if let Some(timestamp) = self.timestamp {
timestamp.to_string()
} else {
"".to_string()
};
Some((date, time))
}
pub fn with_type(mut self, error_type: SystemErrorCodes) -> Self {
self.error.error_type = error_type;
self
}
pub fn with_content(mut self, error_content: String) -> Self {
self.error.error_content = error_content;
self
}
pub fn with_file_data(mut self, file: String) -> Self {
self.file = Some(file);
self
}
pub fn with_location_data(mut self, location: (u32, u32)) -> Self {
self.location = Some(location);
self
}
pub fn with_datestamp_data(mut self, datestamp: NaiveDate) -> Self {
self.datestamp = Some(datestamp);
self
}
pub fn with_timestamp_data(mut self, timestamp: NaiveTime) -> Self {
self.timestamp = Some(timestamp);
self
}
pub fn add_error_type(&mut self, error_type: SystemErrorCodes) {
self.error.error_type = error_type;
}
pub fn add_error_content(&mut self, content: String) {
self.error.error_content = content;
}
pub fn add_file_data(&mut self, file: String) {
self.file = Some(file);
}
pub fn add_location_data(&mut self, location: (u32, u32)) {
self.location = Some(location);
}
pub fn add_datestamp_data(&mut self, datestamp: NaiveDate) {
self.datestamp = Some(datestamp);
}
pub fn add_timestamp_data(&mut self, timestamp: NaiveTime) {
self.timestamp = Some(timestamp);
}
}
impl Display for TheError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
if self.file.is_some() && self.location.is_some() {
write!(f, "{}: {} @{}: {}|{}", self.error.error_type, self.error.error_content, self.file.clone().unwrap(), self.location.unwrap().0, self.location.unwrap().1)
.expect("Couldn't display message!!");
} else {
write!(f, "{}: {}", self.error.error_type, self.error.error_content)
.expect("Couldn't display message!!");
}
Ok(())
}
}
impl From<TheError> for TheErrorType {
fn from(value: TheError) -> Self {
Self {
error_type: value.error.error_type,
error_content: value.error.error_content
}
}
}