extend cherr! macro

This commit is contained in:
Harald Hoyer 2019-01-09 13:22:49 +01:00
parent 7003baaaec
commit 6ad9497512
2 changed files with 35 additions and 7 deletions

View file

@ -1,6 +1,6 @@
[package] [package]
name = "chainerror" name = "chainerror"
version = "0.3.2" version = "0.4.0"
authors = ["Harald Hoyer <harald@redhat.com>"] authors = ["Harald Hoyer <harald@redhat.com>"]
edition = "2018" edition = "2018"
license = "MIT/Apache-2.0" license = "MIT/Apache-2.0"

View file

@ -662,15 +662,31 @@ fn func() -> ChainResult<(), FooError> {
**/ **/
#[macro_export] #[macro_export]
macro_rules! cherr { macro_rules! cherr {
( $k:expr ) => { ( $k:expr ) => ({
ChainError::<_>::new($k, None, Some((line!(), file!()))) ChainError::<_>::new($k, None, Some((line!(), file!())))
}; });
( None, $k:expr ) => { ( None, $k:expr ) => ({
ChainError::<_>::new($k, None, Some((line!(), file!()))) ChainError::<_>::new($k, None, Some((line!(), file!())))
}; });
( $e:expr, $k:expr ) => { ( None, $fmt:expr, $($arg:tt)+ ) => ({
cherr!(None, format!($fmt, $($arg)+ ))
});
( None, $fmt:expr, $($arg:tt)+ ) => ({
cherr!(None, format!($fmt, $($arg)+ ))
});
( $e:ident, $k:expr ) => ({
ChainError::<_>::new($k, Some(Box::from($e)), Some((line!(), file!()))) ChainError::<_>::new($k, Some(Box::from($e)), Some((line!(), file!())))
}; });
( $e:path, $k:expr ) => ({
ChainError::<_>::new($k, Some(Box::from($e)), Some((line!(), file!())))
});
( $e:ident, $fmt:expr, $($arg:tt)+ ) => ({
cherr!($e, format!($fmt, $($arg)+ ))
});
( $e:path, $fmt:expr, $($arg:tt)+ ) => ({
cherr!($e, format!($fmt, $($arg)+ ))
});
} }
/** convenience macro for |e| cherr!(e, format!(…)) /** convenience macro for |e| cherr!(e, format!(…))
@ -768,6 +784,12 @@ macro_rules! mstrerr {
( $t:path, $msg:expr ) => ({ ( $t:path, $msg:expr ) => ({
|e| cherr!(e, $t ($msg.to_string())) |e| cherr!(e, $t ($msg.to_string()))
}); });
( $t:ident, $msg:expr, ) => ({
|e| cherr!(e, $t ($msg.to_string()))
});
( $t:path, $msg:expr, ) => ({
|e| cherr!(e, $t ($msg.to_string()))
});
( $t:ident, $fmt:expr, $($arg:tt)+ ) => ({ ( $t:ident, $fmt:expr, $($arg:tt)+ ) => ({
|e| cherr!(e, $t (format!($fmt, $($arg)+ ))) |e| cherr!(e, $t (format!($fmt, $($arg)+ )))
}); });
@ -828,6 +850,12 @@ macro_rules! strerr {
( $t:path, $msg:expr ) => ({ ( $t:path, $msg:expr ) => ({
cherr!($t ($msg.to_string())) cherr!($t ($msg.to_string()))
}); });
( $t:ident, $msg:expr, ) => ({
cherr!($t ($msg.to_string()))
});
( $t:path, $msg:expr, ) => ({
cherr!($t ($msg.to_string()))
});
( $t:ident, $fmt:expr, $($arg:tt)+ ) => ({ ( $t:ident, $fmt:expr, $($arg:tt)+ ) => ({
cherr!($t (format!($fmt, $($arg)+ ))) cherr!($t (format!($fmt, $($arg)+ )))
}); });