mirror of
https://github.com/jht5945/rust_util.git
synced 2025-12-27 15:40:03 +08:00
feat: add opt_result
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rust_util"
|
name = "rust_util"
|
||||||
version = "0.6.31"
|
version = "0.6.32"
|
||||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "Hatter's Rust Util"
|
description = "Hatter's Rust Util"
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
#[macro_use] extern crate rust_util;
|
#[macro_use] extern crate rust_util;
|
||||||
|
|
||||||
use rust_util::XResult;
|
use rust_util::{XResult, SimpleError};
|
||||||
|
|
||||||
// cargo run --example log
|
// cargo run --example log
|
||||||
fn main() -> XResult<()> {
|
fn main() -> XResult<()> {
|
||||||
@@ -18,5 +18,12 @@ error or ^"##);
|
|||||||
warning!("Hello {}", "world!");
|
warning!("Hello {}", "world!");
|
||||||
failure!("Hello {}", "world!");
|
failure!("Hello {}", "world!");
|
||||||
|
|
||||||
|
println!("{:?}", test_opt_result());
|
||||||
|
|
||||||
simple_error!("helloworld {}", 1)
|
simple_error!("helloworld {}", 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn test_opt_result() -> XResult<()> {
|
||||||
|
let a = Err(SimpleError::new("test".into()));
|
||||||
|
opt_result!(a, "error: {}")
|
||||||
|
}
|
||||||
|
|||||||
16
src/lib.rs
16
src/lib.rs
@@ -41,7 +41,7 @@ pub mod util_tlv;
|
|||||||
($($arg:tt)+) => ( rust_util::util_msg::print_debug(&format!($($arg)+)); )
|
($($arg:tt)+) => ( rust_util::util_msg::print_debug(&format!($($arg)+)); )
|
||||||
}
|
}
|
||||||
#[macro_export] macro_rules! opt_value {
|
#[macro_export] macro_rules! opt_value {
|
||||||
($e: expr) => ( match $e { Some(o) => o, None => return, } )
|
($ex: expr) => ( match $ex { Some(o) => o, None => return, } )
|
||||||
}
|
}
|
||||||
|
|
||||||
pub type XResult<T> = Result<T, Box<dyn Error>>;
|
pub type XResult<T> = Result<T, Box<dyn Error>>;
|
||||||
@@ -54,8 +54,20 @@ pub fn new_box_ioerror(m: &str) -> Box<dyn Error> {
|
|||||||
Box::new(IoError::new(ErrorKind::Other, m))
|
Box::new(IoError::new(ErrorKind::Other, m))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[macro_export] macro_rules! opt_result {
|
||||||
|
($ex: expr, $($arg:tt)+) => (
|
||||||
|
match $ex {
|
||||||
|
Ok(o) => o,
|
||||||
|
Err(e) => return Err(rust_util::SimpleError::new(
|
||||||
|
format!("{}, file: {}, line: {}", format!($($arg)+, e), file!(), line!())
|
||||||
|
).into()),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
#[macro_export] macro_rules! simple_error {
|
#[macro_export] macro_rules! simple_error {
|
||||||
($($arg:tt)+) => ( Err(rust_util::SimpleError::new(format!($($arg)+)).into()) )
|
($($arg:tt)+) => ( Err(rust_util::SimpleError::new(
|
||||||
|
format!("{}, file: {}, line: {}", format!($($arg)+), file!(), line!())
|
||||||
|
).into()) )
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|||||||
Reference in New Issue
Block a user