summaryrefslogtreecommitdiff
path: root/src/result.rs
diff options
context:
space:
mode:
authorBotahamec <botahamec@outlook.com>2023-08-09 10:20:38 -0400
committerBotahamec <botahamec@outlook.com>2023-08-09 10:20:38 -0400
commite187b45c780217faa64bce567ecf5ba20371117c (patch)
tree9b2a5c0bca46a05337218d67de4bccb4f5efa941 /src/result.rs
parente45e83fc37d8f95427a29cb78a274398877f7b05 (diff)
Add ResultNoneExt
Diffstat (limited to 'src/result.rs')
-rw-r--r--src/result.rs70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/result.rs b/src/result.rs
index 0bef098..f31df2b 100644
--- a/src/result.rs
+++ b/src/result.rs
@@ -133,6 +133,76 @@ impl<T, E: Errorable + 'static> ResultMsgExt<T> for Result<T, E> {
}
}
+/// Provides [`Result::unexpect_none`] and [`Option::unexpect_none`]
+///
+/// [`Result::unexpect_none`]: `ResultNoneExt::unexpect_none`
+/// [`Option::unexpect_none`]: `ResultNoneExt::unexpect_none`
+pub trait ResultNoneExt<T>: Sealed {
+ /// Converts [`Result<T, E>`] or [`Option<T>`] to
+ /// [`Result<T, RawUnexpected>`].
+ ///
+ /// # Examples
+ ///
+ /// ```
+ /// use exun::*;
+ /// use core::fmt::Error;
+ ///
+ /// let res: Result<i32, Error> = Err(Error);
+ /// let res: Result<i32, RawUnexpected> = res.unexpect_none();
+ /// ```
+ ///
+ /// Use with the try operator
+ ///
+ /// ```
+ /// use exun::*;
+ /// use core::fmt::Error;
+ ///
+ /// fn foo() -> Result<i32, UnexpectedError> {
+ /// let res: Result<i32, Error> = Err(Error);
+ /// Ok(res.unexpect_none()?)
+ /// }
+ /// ```
+ ///
+ /// Use with the try operator and [`Exun`]
+ ///
+ /// ```
+ /// use exun::*;
+ /// use core::fmt::Error;
+ ///
+ /// fn foo() -> Result<i32, Exun<(), UnexpectedError>> {
+ /// let res: Result<i32, Error> = Err(Error);
+ /// Ok(res.unexpect_none()?)
+ /// }
+ /// ```
+ ///
+ /// Use with [`Option`]
+ ///
+ /// ```
+ /// use exun::*;
+ ///
+ /// fn foo() -> Result<i32, UnexpectedError> {
+ /// let option: Option<i32> = None;
+ /// Ok(option.unexpect_none()?)
+ /// }
+ /// ```
+ ///
+ /// [`Exun`]: `crate::Exun`
+ #[allow(clippy::missing_errors_doc)]
+ fn unexpect_none(self) -> Result<T, RawUnexpected>;
+}
+
+impl<T, E> ResultNoneExt<T> for Result<T, E> {
+ fn unexpect_none(self) -> Result<T, RawUnexpected> {
+ self.map_or_else(|_| Err(RawUnexpected::none()), |val| Ok(val))
+ }
+}
+
+impl<T> ResultNoneExt<T> for Option<T> {
+ fn unexpect_none(self) -> Result<T, RawUnexpected> {
+ self.ok_or_else(RawUnexpected::none)
+ }
+}
+
pub trait ResultExunExt<T, E, U>: Sealed {
/// Converts [`Result<T, Exun<E, U>>`] to [`Option<E>`].
///