From f0bd9a719a6d4f904371d1816784d6944fdb16a2 Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Fri, 1 Jan 2021 18:58:04 +0800 Subject: [PATCH] feat: add simple_error --- Cargo.toml | 2 +- examples/log.rs | 6 +++++- justfile | 7 +++++-- src/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++------ 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index d6cf623..cbc2e05 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_util" -version = "0.6.24" +version = "0.6.25" authors = ["Hatter Jiang "] edition = "2018" description = "Hatter's Rust Util" diff --git a/examples/log.rs b/examples/log.rs index 4e3b856..2115474 100644 --- a/examples/log.rs +++ b/examples/log.rs @@ -1,7 +1,9 @@ #[macro_use] extern crate rust_util; +use rust_util::XResult; + // cargo run --example log -fn main() { +fn main() -> XResult<()> { std::env::set_var("LOGGER_LEVEL", "*"); println!(r##"env LOGGER_LEVEL set to: debug or * @@ -15,4 +17,6 @@ error or ^"##); success!("Hello {}", "world!"); warning!("Hello {}", "world!"); failure!("Hello {}", "world!"); + + simple_error!("helloworld {}", 1) } \ No newline at end of file diff --git a/justfile b/justfile index 34f4f9a..0837260 100644 --- a/justfile +++ b/justfile @@ -1,9 +1,12 @@ -# help -aa: +_: @just --list # example log log: cargo run --example log +# publish +publish: + cargo publish + diff --git a/src/lib.rs b/src/lib.rs index 3ce8f2f..d505a62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,12 @@ extern crate lazy_static; extern crate term; -use std::io::{ Error, ErrorKind }; +use std::error::Error; +use std::io::Error as IoError; +use std::io::ErrorKind; +use std::fmt::Display; +use std::fmt::Formatter; +use std::fmt::Result as FmtResult; pub mod util_io; pub mod util_os; @@ -40,12 +45,44 @@ pub mod util_git; ($e: expr) => ( match $e { Some(o) => o, None => return, } ) } -pub type XResult = Result>; +pub type XResult = Result>; -pub fn new_box_error(m: &str) -> Box { - Box::new(Error::new(ErrorKind::Other, m)) +pub fn new_box_error(m: &str) -> Box { + Box::new(IoError::new(ErrorKind::Other, m)) } -pub fn new_box_ioerror(m: &str) -> Box { - Box::new(Error::new(ErrorKind::Other, m)) +pub fn new_box_ioerror(m: &str) -> Box { + Box::new(IoError::new(ErrorKind::Other, m)) } + +#[macro_export] macro_rules! simple_error { + ($($arg:tt)+) => ( Err(rust_util::SimpleError::new(format!($($arg)+)).into()) ) +} + +#[derive(Debug)] +pub struct SimpleError { + pub message: String, + pub source: Option>, +} + +impl SimpleError { + pub fn new(message: String) -> Self { + Self { message, source: None } + } + + pub fn new2(message: String, source: Box) -> Self { + Self { message, source: Some(source) } + } +} + +impl Display for SimpleError { + fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult { + match &self.source { + None => write!(f, "SimpleErorr, message: {}", self.message), + Some(e) => write!(f, "SimpleErorr, message: {}, source erorr: {}", self.message, e), + } + } +} + +impl Error for SimpleError {} +