From ac66ebf6cc800b6ba4da0a7cd0f29dfb1ad9a377 Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Sun, 21 Jan 2024 23:19:00 +0100 Subject: [PATCH] Fixed how unexpected Option errors were being worded --- src/error/unexpected.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/error/unexpected.rs b/src/error/unexpected.rs index 00c5b8c..71e82fc 100644 --- a/src/error/unexpected.rs +++ b/src/error/unexpected.rs @@ -105,16 +105,19 @@ impl Mappable for result::Result { } } -static OPTION_NONE_ERROR: &str = "expected Some but got None"; +#[derive(thiserror::Error, Debug)] +enum OptionError { + #[error("required but not given")] + None, +} impl Mappable for Option { fn or_unexpected(self) -> Result { - self.ok_or(Error::from(OPTION_NONE_ERROR)).or_unexpected() + self.ok_or(OptionError::None).or_unexpected() } fn or_unexpected_while(self, prefix: D) -> Result { - self.ok_or(Error::from(OPTION_NONE_ERROR)) - .or_unexpected_while(prefix) + self.ok_or(OptionError::None).or_unexpected_while(prefix) } fn map_unexpected_while(self, f: F) -> Result @@ -122,8 +125,7 @@ impl Mappable for Option { F: FnOnce() -> D, D: fmt::Display, { - self.ok_or(Error::from(OPTION_NONE_ERROR)) - .map_unexpected_while(f) + self.ok_or(OptionError::None).map_unexpected_while(f) } }