Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend Option with ok_or_eyre? #125

Closed
LeoniePhiline opened this issue Nov 21, 2023 · 2 comments
Closed

Extend Option with ok_or_eyre? #125

LeoniePhiline opened this issue Nov 21, 2023 · 2 comments

Comments

@LeoniePhiline
Copy link
Contributor

LeoniePhiline commented Nov 21, 2023

When discussing https://docs.rs/eyre/latest/eyre/index.html#context-and-option, a user remarked that a closure and macro invocation just for a static string error (as documented: opt.ok_or_else(|| eyre!("new error message"))) seemed cumbersome.

The reasoning at the documentation's "Context and Option" section does make sense - WrapErr would indicate wrapping an error, where there is (literally) None. This issue is not asking to implement WrapErr for Option.

An extension trait, offering ok_or_eyre on the Option type does seem useful to avoid closure + macro boilerplate:

use std::fmt::{Debug, Display};

pub trait OptionExt<T> {
    /// Example usage:
    ///
    /// ```rust
    /// let option: Option<()> = None;
    ///
    /// let result = option.ok_or_eyre("static str error");
    ///
    /// assert_eq!(result.unwrap_err().to_string(), "static str error");
    /// ```
    fn ok_or_eyre<M>(self, message: M) -> crate::Result<T>
    where
        M: Debug + Display + Send + Sync + 'static;
}

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

Is this something you would consider accepting as contribution? Names are of course up for bikeshedding.

@yaahc
Copy link
Collaborator

yaahc commented Dec 5, 2023

yes! This seems like a fantastic way to navigate that concern; I'd definitely not object to this addition.

LeoniePhiline added a commit to LeoniePhiline/eyre that referenced this issue Dec 6, 2023
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 eyre-rs#125
LeoniePhiline added a commit to LeoniePhiline/eyre that referenced this issue Dec 6, 2023
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 eyre-rs#125
LeoniePhiline added a commit to LeoniePhiline/eyre that referenced this issue Dec 6, 2023
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 eyre-rs#125
LeoniePhiline added a commit to LeoniePhiline/eyre that referenced this issue Dec 6, 2023
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 eyre-rs#125
LeoniePhiline added a commit to LeoniePhiline/eyre that referenced this issue Dec 6, 2023
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 eyre-rs#125
LeoniePhiline added a commit to LeoniePhiline/eyre that referenced this issue Dec 6, 2023
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 eyre-rs#125
LeoniePhiline added a commit to LeoniePhiline/eyre that referenced this issue Dec 6, 2023
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 eyre-rs#125
LeoniePhiline added a commit to LeoniePhiline/eyre that referenced this issue Dec 6, 2023
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 eyre-rs#125
ten3roberts pushed a commit that referenced this issue Dec 6, 2023
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
@LeoniePhiline
Copy link
Contributor Author

Landed with #129's merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants