tuwunel_core/utils/result/map_expect.rs
1use std::fmt::Debug;
2
3use super::Result;
4
5/// Applies an expectation to a nested optional or fallible value.
6///
7/// Implementations cover `Option<Result<T, E>>` and `Result<Option<T>, E>`.
8/// Only the nested value is unwrapped, preserving the outer container.
9pub trait MapExpect<'a, T> {
10 /// Unwraps the nested value with the supplied expectation message.
11 ///
12 /// The outer `Option` or `Result` is preserved. The message is used only
13 /// when the nested value is absent or failed.
14 ///
15 /// # Panics
16 ///
17 /// Panics when the nested value does not contain a success value.
18 fn map_expect(self, msg: &'a str) -> T;
19}
20
21impl<'a, T, E: Debug> MapExpect<'a, Option<T>> for Option<Result<T, E>> {
22 #[inline]
23 fn map_expect(self, msg: &'a str) -> Option<T> { self.map(|result| result.expect(msg)) }
24}
25
26impl<'a, T, E: Debug> MapExpect<'a, Result<T, E>> for Result<Option<T>, E> {
27 #[inline]
28 fn map_expect(self, msg: &'a str) -> Result<T, E> { self.map(|result| result.expect(msg)) }
29}