use core::fmt::Debug; #[cfg(feature = "std")] use std::error::Error; use crate::{unexpected::Errorable, Exun, RawUnexpected}; mod sealed { pub trait Sealed {} impl Sealed for Result {} impl Sealed for Option {} } use sealed::Sealed; /// Provides [`Result::unexpect`] /// /// [`Result::unexpect`]: `ResultErrorExt::unexpect` #[cfg(feature = "std")] pub trait ResultErrorExt: Sealed { /// Converts [`Result`] to [`Result`]. /// /// # Examples /// /// Basic usage: /// /// ``` /// use exun::*; /// use core::fmt::Error; /// /// let res: Result = Err(Error); /// let res: Result = res.unexpect(); /// ``` /// /// Use with the try operator /// /// ``` /// use exun::*; /// use core::fmt::Error; /// /// fn foo() -> Result { /// let res: Result = Err(Error); /// Ok(res.unexpect()?) /// } /// ``` /// /// Use with the try operator and [`Exun`] /// /// ``` /// use exun::*; /// use core::fmt::Error; /// /// fn foo() -> Result> { /// let res: Result = Err(Error); /// Ok(res.unexpect()?) /// } /// ``` /// /// [`Exun`]: `crate::Exun` #[allow(clippy::missing_errors_doc)] fn unexpect(self) -> Result; } #[cfg(feature = "std")] impl ResultErrorExt for Result { fn unexpect(self) -> Result { self.map_err(RawUnexpected::new) } } impl ResultErrorExt for Result { fn unexpect(self) -> Self { self } } impl ResultErrorExt for Option { fn unexpect(self) -> Result { self.ok_or_else(RawUnexpected::none) } } /// Provides [`Result::unexpect_msg`] /// /// [`Result::unexpect_msg`]: `ResultMsgExt::unexpect_msg` pub trait ResultMsgExt: Sealed { /// Converts [`Result`] to [`Result`]. /// /// This is provided for compatibility with `no_std`. If your type /// implements [`Error`], then you should prefer that instead. /// /// # Examples /// /// Basic usage: /// /// ``` /// use exun::*; /// /// let res: Result = Err("failure"); /// let res: Result = res.unexpect_msg(); /// ``` /// /// Use with the try operator /// /// ``` /// use exun::*; /// /// fn foo() -> Result { /// let res: Result = Err("failure"); /// Ok(res.unexpect_msg()?) /// } /// ``` /// /// Use with the try operator and [`Exun`] /// /// ``` /// use exun::*; /// /// fn foo() -> Result> { /// let res: Result = Err("failure"); /// Ok(res.unexpect_msg()?) /// } /// ``` /// /// [`Exun`]: `crate::Exun` #[allow(clippy::missing_errors_doc)] fn unexpect_msg(self) -> Result; } impl ResultMsgExt for Result { fn unexpect_msg(self) -> Result { self.map_err(RawUnexpected::msg) } } trait ResultExunExt: Sealed { fn expected_err(self) -> Option; fn unexpected_err(self) -> Option; fn map_expected_err(self, op: impl FnOnce(E) -> F) -> Result>; fn map_unexpected_err(self, op: impl FnOnce(U) -> F) -> Result>; fn unwrap_result(self) -> Result where U: Debug; fn unwrap_expected_err(self) -> E where T: Debug, U: Debug; fn unwrap_unexpected_err(self) -> U where T: Debug, E: Debug; } impl ResultExunExt for Result> { fn expected_err(self) -> Option { self.err()?.expected() } fn unexpected_err(self) -> Option { self.err()?.unexpected() } fn map_expected_err(self, op: impl FnOnce(E) -> F) -> Result> { self.map_err(|e| e.map(op)) } fn map_unexpected_err(self, op: impl FnOnce(U) -> F) -> Result> { self.map_err(|e| e.map_unexpected(op)) } fn unwrap_result(self) -> Result where U: Debug, { match self { Ok(value) => Ok(value), Err(error) => Err(error.unwrap()), } } fn unwrap_expected_err(self) -> E where T: Debug, U: Debug, { self.unwrap_err().unwrap() } fn unwrap_unexpected_err(self) -> U where T: Debug, E: Debug, { self.unwrap_err().unwrap_unexpected() } }