From 320c1e4bd8d93ef75466846b111c6b66b59da3ce Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 27 Jun 2016 15:12:17 +0200 Subject: [PATCH] Add Option helper --- libimagerror/src/error_gen.rs | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/libimagerror/src/error_gen.rs b/libimagerror/src/error_gen.rs index a9939ae5..048facb5 100644 --- a/libimagerror/src/error_gen.rs +++ b/libimagerror/src/error_gen.rs @@ -151,6 +151,37 @@ macro_rules! generate_result_helper { } } +#[macro_export] +macro_rules! generate_option_helper { + ( + $name: ident, + $kindname: ident + ) => { + /// Trait to replace + /// + /// ```ignore + /// foo.ok_or(SomeType::SomeErrorKind.into_error()) + /// ``` + /// + /// with + /// + /// ```ignore + /// foo.ok_or_errkind(SomeType::SomeErrorKind) + /// ``` + pub trait OkOrErr { + fn ok_or_errkind(self, kind: $kindname) -> Result; + } + + impl OkOrErr for Option { + + fn ok_or_errkind(self, kind: $kindname) -> Result { + self.ok_or(kind.into_error()) + } + + } + } +} + #[macro_export] macro_rules! generate_error_types { ( @@ -165,6 +196,7 @@ macro_rules! generate_error_types { $($kind => $string),*); generate_result_helper!($name, $kindname); + generate_option_helper!($name, $kindname); } }