diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/result.rs | 7 | ||||
| -rw-r--r-- | src/unexpected.rs | 47 |
2 files changed, 54 insertions, 0 deletions
diff --git a/src/result.rs b/src/result.rs index 7afceed..25392c8 100644 --- a/src/result.rs +++ b/src/result.rs @@ -6,6 +6,7 @@ use crate::{unexpected::Errorable, RawUnexpected}; mod sealed { pub trait Sealed {} impl<T, E> Sealed for Result<T, E> {} + impl<T> Sealed for Option<T> {} } use sealed::Sealed; @@ -71,6 +72,12 @@ impl<T> ResultErrorExt<T> for Result<T, RawUnexpected> { } } +impl<T> ResultErrorExt<T> for Option<T> { + fn unexpect(self) -> Result<T, RawUnexpected> { + self.ok_or_else(RawUnexpected::none) + } +} + /// Provides [`Result::unexpect_msg`] /// /// [`Result::unexpect_msg`]: `ResultMsgExt::unexpect_msg` diff --git a/src/unexpected.rs b/src/unexpected.rs index 321973f..6538b96 100644 --- a/src/unexpected.rs +++ b/src/unexpected.rs @@ -11,6 +11,7 @@ impl<T: Display + Debug + Send + Sync + ?Sized> Errorable for T {} #[derive(Debug)] enum ErrorTy { + None, Message(Box<dyn Errorable + 'static>), #[cfg(feature = "std")] Error(Box<dyn Error + Send + Sync + 'static>), @@ -30,6 +31,7 @@ pub struct RawUnexpected { impl Display for RawUnexpected { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match &self.internal { + ErrorTy::None => Display::fmt("Called `unexpect` on a `None` value", f), ErrorTy::Message(m) => Display::fmt(&m, f), #[cfg(feature = "std")] ErrorTy::Error(e) => Display::fmt(&e, f), @@ -60,6 +62,7 @@ impl RawUnexpected { /// let x = RawUnexpected::new(core::fmt::Error); /// ``` #[cfg(feature = "std")] + #[must_use] pub fn new<E: Error + Send + Sync + 'static>(error: E) -> Self { Self { internal: ErrorTy::Error(Box::new(error)), @@ -80,12 +83,34 @@ impl RawUnexpected { /// /// let x = RawUnexpected::msg("failed"); /// ``` + #[must_use] pub fn msg<E: Display + Debug + Send + Sync + 'static>(error: E) -> Self { Self { internal: ErrorTy::Message(Box::new(error)), } } + /// Create a new `RawUnexpected` that is simply empty. + /// + /// This is used for converting an [`Option<T>`] to a + /// [`Result<T, RawUnexpected`]. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// use exun::*; + /// + /// let x = RawUnexpected::none(); + /// ``` + #[must_use] + pub fn none() -> Self { + Self { + internal: ErrorTy::None, + } + } + /// Get the original error. /// /// This will return `None` if `self` was created using @@ -108,6 +133,7 @@ impl RawUnexpected { #[cfg(feature = "std")] pub fn source(&self) -> Option<&(dyn Error + 'static)> { match &self.internal { + ErrorTy::None => None, ErrorTy::Message(_) => None, #[cfg(feature = "std")] ErrorTy::Error(e) => Some(&**e), @@ -138,6 +164,7 @@ impl UnexpectedError { /// let x = UnexpectedError::new(core::fmt::Error); /// ``` #[cfg(feature = "std")] + #[must_use] pub fn new<E: Error + Send + Sync + 'static>(error: E) -> Self { Self(RawUnexpected::new(error)) } @@ -156,9 +183,29 @@ impl UnexpectedError { /// /// let x = UnexpectedError::msg("failed"); /// ``` + #[must_use] pub fn msg<E: Display + Debug + Send + Sync + 'static>(error: E) -> Self { Self(RawUnexpected::msg(error)) } + + /// Create a new `RawUnexpected` that is simply empty. + /// + /// This is used for converting an [`Option<T>`] to a + /// [`Result<T, RawUnexpected`]. + /// + /// # Examples + /// + /// Basic usage: + /// + /// ``` + /// use exun::*; + /// + /// let x = UnexpectedError::none(); + /// ``` + #[must_use] + pub fn none() -> Self { + Self(RawUnexpected::none()) + } } impl From<RawUnexpected> for UnexpectedError { |
