exception 0.0.1

简单的异常统一处理
Documentation
use std::{error::Error, fmt};

pub use chrono::{DateTime, Local};
use colored::Colorize;


/// 异常结构体,用于保存异常相关信息
///
/// # Examples
/// ```
/// use exception::Exception;
/// let ex = except!("Here is an exception.");
/// ex.report();
/// panic!("{}", ex);
/// ```
#[derive(Debug, Clone)]
pub struct Exception {
    pub time: DateTime<Local>,
    pub msg: String,
    pub file: String,
    pub line: u32,
}

impl Exception {
	pub fn new<S>(msg: S, file: S, line: u32) -> Exception
    where
        S: ToString,
    {
        Exception {
            time: Local::now(),
            msg: msg.to_string(),
            file: file.to_string(),
            line,
        }
    }
    pub fn pack<E,S>(err: E, file: S, line: u32) -> Exception
    where
		E: Error,
		S: ToString
    {
        Exception {
            time: Local::now(),
            msg: err.to_string(),
            file: file.to_string(),
            line,
        }
    }

	pub fn report(&self) {
		eprint!("{}:{}", "error".red().bold(), self)
	}
}

impl Error for Exception {}

impl fmt::Display for Exception {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "\n\t time: {},\n\t  msg: {}\n\twhere: {}\n",
            self.time,
            self.msg,
            format!("{}:{}", self.file, self.line)
        )
    }
}

impl From<std::io::Error> for Exception {
	fn from(value: std::io::Error) -> Self {
		Exception::pack(value, "<Can not captured>", 0)
	}
}