mod macros;
mod e_msg;
mod flex_impl;
mod easy;
mod null_pointer;
mod builder;
mod index;
mod sql;
mod unsupported;
pub use easy::EasyException;
pub use null_pointer::NullPointerException;
pub use index::ArrayIndexOutOfBoundsException;
pub use unsupported::UnSupportedOpException;
pub use sql::SQLException;
pub use flex_impl::*;
pub use e_msg::*;
pub use builder::*;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use std::error::Error;
use std::fmt::{Debug, Display, Formatter};
use crate::{display_err_impl, exception_impl};
pub trait Exception: Error {
fn code(&self) -> u32;
fn msg(&self) -> &str;
fn level(&self) -> ExceptionLevel;
fn set_code(&mut self, code: u32) -> ();
fn set_msg(&mut self, msg: &str) -> ();
fn set_level(&mut self, level: ExceptionLevel) -> ();
fn get_type(&self) -> Exceptions;
fn timestamp(&self) -> Duration;
}
#[derive(Debug, Clone, PartialEq)]
pub enum Exceptions {
Super,
Easy,
NullPointer,
ArrayIndexOutOfBounds,
IllegalArgState,
IO,
FileNotFound,
SQL,
Interrupted,
ClassCast,
UnSupportedOperation,
Define,
}
impl Default for Exceptions{
fn default() -> Self {
Exceptions::Define
}
}
pub struct ExceptionCode(u32);
impl ExceptionCode {
pub const COMMON: u32 = 101;
pub const SUPER: u32 = 102;
pub const NULL_POINTER: u32 = 1000;
pub const ARRAY_INDEX_OUT_OF: u32 = 1100;
pub const ILLEGAL_ARGUMENT: u32 = 1200;
pub const ILLEGAL_STATE: u32 = 1300;
pub const IO: u32 = 1400;
pub const FILE_NOT_FOUND: u32 = 1500;
pub const SQL: u32 = 1600;
pub const INTERRUPTED: u32 = 1700;
pub const CLASS_CAST: u32 = 1800;
pub const UNSUPPORTED_OPERATION: u32 = 1900;
pub fn define(&mut self, code: u32) -> ExceptionCode {
Self(code)
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum ExceptionLevel {
Error,
Fatal,
Warn,
Debug,
Info,
Trace,
}
impl Default for ExceptionLevel{
fn default() -> Self {
ExceptionLevel::Info
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum Reasons {
UnSupported(UnSupportedReasons),
SQL(SQLReasons),
Other(String),
}
impl Default for Reasons{
fn default() -> Self {
Reasons::Other(String::new())
}
}
#[derive(Debug, PartialEq, Clone)]
pub enum UnSupportedReasons {
Value,
Type,
Block,
Lock,
UnAccessible,
Auth,
IO,
FileNotFound,
Interrupted,
ClassCast,
Other,
}
#[derive(Debug, PartialEq, Clone)]
pub enum SQLReasons {
Stmt,
Logic,
NO_DB,
NO_Table,
NO_Column,
NO_Rows,
Empty,
NO_Auth,
NO_Namespace,
Insert,
Update,
Query,
Delete,
Other,
}
#[derive(Debug, Clone, PartialEq)]
pub struct SuperException {
code: u32,
msg: String,
level: ExceptionLevel,
timestamp: Duration,
}
impl NewFrom for SuperException {
type Builder = SuperBuilder;
fn new() -> Self::Builder {
SuperBuilder::new()
}
fn from_super(e: Box<dyn Exception>) -> Self where Self: Sized {
SuperException {
code: e.code(),
msg: String::from(e.msg()),
level: e.level(),
timestamp: e.timestamp(),
}
}
}
impl FromBuilder for SuperException {
type Output = SuperException;
type Input = SuperBuilder;
fn from_builder(builder: &Self::Input) -> Self::Output {
Self::Output {
code: builder.code(),
msg: String::from(builder.msg()),
level: builder.level(),
timestamp: builder.timestamp(),
}
}
}
display_err_impl!(SuperException);
exception_impl!(SuperException,Exceptions::Super);
impl DerefException for SuperException {
fn deref_mut_exception(&mut self) -> Self {
SuperException {
code: self.code(),
msg: String::from(self.msg()),
level: self.level(),
timestamp: self.timestamp(),
}
}
}
impl Default for SuperException {
fn default() -> Self {
SuperException {
code: ExceptionCode::SUPER,
msg: String::from(SUPER_MSG),
level: ExceptionLevel::Info,
timestamp: SystemTime::now().duration_since(UNIX_EPOCH).unwrap(),
}
}
}