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); } }