From 0d9d96fa6c9f8091721d90c8b19909883a47eee6 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 27 Jun 2016 15:12:08 +0200 Subject: [PATCH] Add result helper --- libimagerror/src/error_gen.rs | 37 +++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/libimagerror/src/error_gen.rs b/libimagerror/src/error_gen.rs index 9511c988..a9939ae5 100644 --- a/libimagerror/src/error_gen.rs +++ b/libimagerror/src/error_gen.rs @@ -116,6 +116,41 @@ macro_rules! generate_custom_error_types { } } +#[macro_export] +macro_rules! generate_result_helper { + ( + $name: ident, + $kindname: ident + ) => { + /// Trait to replace + /// + /// ```ignore + /// foo.map_err(Box::new).map_err(|e| SomeType::SomeErrorKind.into_error_with_cause(e)) + /// // or: + /// foo.map_err(|e| SomeType::SomeErrorKind.into_error_with_cause(Box::new(e))) + /// ``` + /// + /// with much nicer + /// + /// ```ignore + /// foo.map_err_into(SomeType::SomeErrorKind) + /// ``` + /// + pub trait MapErrInto { + fn map_err_into(self, error_kind: $kindname) -> Result; + } + + impl MapErrInto for Result { + + fn map_err_into(self, error_kind: $kindname) -> Result { + self.map_err(Box::new) + .map_err(|e| error_kind.into_error_with_cause(e)) + } + + } + } +} + #[macro_export] macro_rules! generate_error_types { ( @@ -128,6 +163,8 @@ macro_rules! generate_error_types { generate_custom_error_types!($name, $kindname, SomeNotExistingTypeWithATypeNameNoOneWillEverChoose, $($kind => $string),*); + + generate_result_helper!($name, $kindname); } }