Fixed how unexpected Option errors were being worded

This commit is contained in:
Brian Picciano 2024-01-21 23:19:00 +01:00
parent 78b67a02ad
commit ac66ebf6cc

View File

@ -105,16 +105,19 @@ impl<T, E: error::Error> Mappable<T> for result::Result<T, E> {
} }
} }
static OPTION_NONE_ERROR: &str = "expected Some but got None"; #[derive(thiserror::Error, Debug)]
enum OptionError {
#[error("required but not given")]
None,
}
impl<T> Mappable<T> for Option<T> { impl<T> Mappable<T> for Option<T> {
fn or_unexpected(self) -> Result<T> { fn or_unexpected(self) -> Result<T> {
self.ok_or(Error::from(OPTION_NONE_ERROR)).or_unexpected() self.ok_or(OptionError::None).or_unexpected()
} }
fn or_unexpected_while<D: fmt::Display>(self, prefix: D) -> Result<T> { fn or_unexpected_while<D: fmt::Display>(self, prefix: D) -> Result<T> {
self.ok_or(Error::from(OPTION_NONE_ERROR)) self.ok_or(OptionError::None).or_unexpected_while(prefix)
.or_unexpected_while(prefix)
} }
fn map_unexpected_while<F, D>(self, f: F) -> Result<T> fn map_unexpected_while<F, D>(self, f: F) -> Result<T>
@ -122,8 +125,7 @@ impl<T> Mappable<T> for Option<T> {
F: FnOnce() -> D, F: FnOnce() -> D,
D: fmt::Display, D: fmt::Display,
{ {
self.ok_or(Error::from(OPTION_NONE_ERROR)) self.ok_or(OptionError::None).map_unexpected_while(f)
.map_unexpected_while(f)
} }
} }