use std::time::{SystemTime,Duration,UNIX_EPOCH};
use std::path::PathBuf;
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use super::{NewFrom, FromBuilder, EasyExceptionBuilder, CommonParamImpl, EASY_MSG, ExceptionCode, SuperBuilderImpl};
use crate::{DerefException, display_err_impl, Exception, exception_impl, common_param_impl, ExceptionLevel, Exceptions};
#[derive(Debug, Clone, PartialEq)]
pub struct EasyException {
code: u32,
msg: String,
line: u32,
path: PathBuf,
level: ExceptionLevel,
timestamp: Duration,
}
impl Default for EasyException {
fn default() -> Self {
EasyException {
code: ExceptionCode::COMMON,
msg: String::from(EASY_MSG),
line: 0,
path: PathBuf::new(),
level: ExceptionLevel::Info,
timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
}
}
}
impl NewFrom for EasyException {
type Builder = EasyExceptionBuilder;
fn new() -> Self::Builder {
EasyExceptionBuilder::new()
}
fn from_super(e: Box<dyn Exception>) -> Self where Self: Sized {
EasyException {
code: e.code(),
msg: String::from(e.msg()),
level: e.level(),
path: PathBuf::new(),
line: 0,
timestamp: e.timestamp()
}
}
}
impl FromBuilder for EasyException {
type Input = EasyExceptionBuilder;
type Output = EasyException;
fn from_builder(builder: &Self::Input) -> Self::Output {
Self::Output {
code: builder.code(),
msg: String::from(builder.msg()),
level: builder.level(),
line: builder.line(),
path: builder.path(),
timestamp: builder.timestamp()
}
}
}
display_err_impl!(EasyException);
exception_impl!(EasyException,Exceptions::Easy);
common_param_impl!(EasyException);
impl DerefException for EasyException {
fn deref_mut_exception(&mut self) -> Self {
EasyException {
code: self.code(),
msg: String::from(self.msg()),
line: self.line(),
path: self.path(),
level: self.level(),
timestamp: self.timestamp()
}
}
}