Add Option helper
This commit is contained in:
parent
0d9d96fa6c
commit
320c1e4bd8
1 changed files with 32 additions and 0 deletions
|
@ -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<T> {
|
||||
fn ok_or_errkind(self, kind: $kindname) -> Result<T, $name>;
|
||||
}
|
||||
|
||||
impl<T> OkOrErr<T> for Option<T> {
|
||||
|
||||
fn ok_or_errkind(self, kind: $kindname) -> Result<T, $name> {
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue