-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Previously, a closure and macro invocation was required to generate a static string error object from an `Option::None`. This change adds an extension trait, providing the `ok_or_eyre` method on the `Option` type. `Option::ok_or_eyre` accepts static error messages and creates `Report` objects lazily in the `None` case. Implements #125
- Loading branch information
1 parent
da84e8c
commit 4e1f323
Showing
5 changed files
with
108 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use crate::OptionExt; | ||
use core::fmt::{Debug, Display}; | ||
|
||
impl<T> OptionExt<T> for Option<T> { | ||
fn ok_or_eyre<M>(self, message: M) -> crate::Result<T> | ||
where | ||
M: Debug + Display + Send + Sync + 'static, | ||
{ | ||
match self { | ||
Some(ok) => Ok(ok), | ||
None => Err(crate::Report::msg(message)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
mod common; | ||
|
||
use self::common::maybe_install_handler; | ||
use eyre::OptionExt; | ||
|
||
#[test] | ||
fn test_option_ok_or_eyre() { | ||
maybe_install_handler().unwrap(); | ||
|
||
let option: Option<()> = None; | ||
|
||
let result = option.ok_or_eyre("static str error"); | ||
|
||
assert_eq!(result.unwrap_err().to_string(), "static str error"); | ||
} |