From 568a2173d5c70151a8210a7ba2137125f93d503d Mon Sep 17 00:00:00 2001 From: Hatter Jiang Date: Sun, 13 Jun 2021 20:31:43 +0800 Subject: [PATCH] feat: add opt_value_result! --- Cargo.toml | 2 +- demo/test_clap/src/main.rs | 6 ++++++ src/lib.rs | 11 +++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d8d1a0f..6511d80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rust_util" -version = "0.6.39" +version = "0.6.40" authors = ["Hatter Jiang "] edition = "2018" description = "Hatter's Rust Util" diff --git a/demo/test_clap/src/main.rs b/demo/test_clap/src/main.rs index 8bf0c50..6dc44bb 100644 --- a/demo/test_clap/src/main.rs +++ b/demo/test_clap/src/main.rs @@ -13,6 +13,12 @@ impl Command for TestCommand { fn run(&self, _arg_matches: &ArgMatches, _: &ArgMatches) -> CommandError { println!("hello test!"); + let a: Option = None; + let b = move || -> rust_util::XResult { + Ok(rust_util::opt_value_result!(a, "test: {}", 1)) + }; + let c = b(); + println!("{:?}", c); Ok(None) } } diff --git a/src/lib.rs b/src/lib.rs index 5fefe9d..586943d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -66,6 +66,17 @@ pub fn new_box_ioerror(m: &str) -> Box { 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 { ($ex: expr, $($arg:tt)+) => ( match $ex {