mirror of
https://github.com/jht5945/rust_util.git
synced 2025-12-27 15:40:03 +08:00
feat: add opt_value_result!
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rust_util"
|
name = "rust_util"
|
||||||
version = "0.6.39"
|
version = "0.6.40"
|
||||||
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"
|
||||||
|
|||||||
@@ -13,6 +13,12 @@ impl Command for TestCommand {
|
|||||||
|
|
||||||
fn run(&self, _arg_matches: &ArgMatches, _: &ArgMatches) -> CommandError {
|
fn run(&self, _arg_matches: &ArgMatches, _: &ArgMatches) -> CommandError {
|
||||||
println!("hello test!");
|
println!("hello test!");
|
||||||
|
let a: Option<String> = None;
|
||||||
|
let b = move || -> rust_util::XResult<String> {
|
||||||
|
Ok(rust_util::opt_value_result!(a, "test: {}", 1))
|
||||||
|
};
|
||||||
|
let c = b();
|
||||||
|
println!("{:?}", c);
|
||||||
Ok(None)
|
Ok(None)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
11
src/lib.rs
11
src/lib.rs
@@ -66,6 +66,17 @@ 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_value_result {
|
||||||
|
($ex: expr, $($arg:tt)+) => (
|
||||||
|
match $ex {
|
||||||
|
Some(o) => o,
|
||||||
|
None => return Err(rust_util::SimpleError::new(
|
||||||
|
format!("{}, file: {}, line: {}", format!($($arg)+), file!(), line!())
|
||||||
|
).into()),
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[macro_export] macro_rules! opt_result {
|
#[macro_export] macro_rules! opt_result {
|
||||||
($ex: expr, $($arg:tt)+) => (
|
($ex: expr, $($arg:tt)+) => (
|
||||||
match $ex {
|
match $ex {
|
||||||
|
|||||||
Reference in New Issue
Block a user