-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathoption_ext.rs
39 lines (35 loc) Β· 1.3 KB
/
option_ext.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use super::*;
/// We currently use `anyhow` for error handling but are migrating to typed
/// errors using `snafu`. This trait exists to provide access to
/// `snafu::OptionExt::{context, with_context}`, which are otherwise shadowed
/// by `anyhow::Context::{context, with_context}`. Once the migration is
/// complete, this trait can be deleted, and `snafu::OptionExt` used directly.
pub trait OptionExt<T>: Sized {
fn snafu_context<C, E>(self, context: C) -> Result<T, E>
where
C: snafu::IntoError<E, Source = snafu::NoneError>,
E: std::error::Error + snafu::ErrorCompat;
#[allow(unused)]
fn with_snafu_context<F, C, E>(self, context: F) -> Result<T, E>
where
F: FnOnce() -> C,
C: snafu::IntoError<E, Source = snafu::NoneError>,
E: std::error::Error + snafu::ErrorCompat;
}
impl<T> OptionExt<T> for Option<T> {
fn snafu_context<C, E>(self, context: C) -> Result<T, E>
where
C: snafu::IntoError<E, Source = snafu::NoneError>,
E: std::error::Error + snafu::ErrorCompat,
{
snafu::OptionExt::context(self, context)
}
fn with_snafu_context<F, C, E>(self, context: F) -> Result<T, E>
where
F: FnOnce() -> C,
C: snafu::IntoError<E, Source = snafu::NoneError>,
E: std::error::Error + snafu::ErrorCompat,
{
snafu::OptionExt::with_context(self, context)
}
}