From 811aef209a5fa4a3192307a2a2ad01b9c5fb310e Mon Sep 17 00:00:00 2001 From: Brian Picciano Date: Wed, 12 Jul 2023 19:01:15 +0200 Subject: [PATCH] introduce unexpected::Result --- src/error/unexpected.rs | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/error/unexpected.rs b/src/error/unexpected.rs index dadd9b8..77420ce 100644 --- a/src/error/unexpected.rs +++ b/src/error/unexpected.rs @@ -1,5 +1,5 @@ use std::fmt::Write; -use std::{error, fmt}; +use std::{error, fmt, result}; #[derive(Debug, Clone, PartialEq)] /// Error is a String which implements the Error trait. It is intended to be used in @@ -10,6 +10,8 @@ use std::{error, fmt}; /// async situations. pub struct Error(String); +pub type Result = result::Result; + impl Error { fn from_displays(prefix: Option, body: &D2, source: Option) -> Error where @@ -55,24 +57,24 @@ impl error::Error for Error {} pub trait Mappable { /// or_unexpected returns an Err(Error) wrapping self's Err, or the Ok value of self. - fn or_unexpected(self) -> Result; + fn or_unexpected(self) -> Result; /// or_unexpected_while is like or_unexpected, but will prefix the error message. The prefix /// should be worded as if it started with the word "while", e.g.: `opening file {path}`. - fn or_unexpected_while(self, prefix: D) -> Result; + fn or_unexpected_while(self, prefix: D) -> Result; /// map_unexpected_while is like or_unexpected_while, but uses a closure to produce the error /// prefix. - fn map_unexpected_while(self, f: F) -> Result + fn map_unexpected_while(self, f: F) -> Result where F: FnOnce() -> D, D: fmt::Display; } fn map_unexpected_maybe_while( - res: Result, + res: result::Result, prefix_fn: Option, -) -> Result +) -> Result where E: error::Error, F: FnOnce() -> D, @@ -84,17 +86,17 @@ where } } -impl Mappable for Result { - fn or_unexpected(self) -> Result { +impl Mappable for result::Result { + fn or_unexpected(self) -> Result { let no_fn = None:: Box>>; // lol, good job rust map_unexpected_maybe_while(self, no_fn) } - fn or_unexpected_while(self, prefix: D) -> Result { + fn or_unexpected_while(self, prefix: D) -> Result { map_unexpected_maybe_while(self, Some(|| prefix)) } - fn map_unexpected_while(self, f: F) -> Result + fn map_unexpected_while(self, f: F) -> Result where F: FnOnce() -> D, D: fmt::Display, @@ -106,16 +108,16 @@ impl Mappable for Result { static OPTION_NONE_ERROR: &str = "expected Some but got None"; impl Mappable for Option { - fn or_unexpected(self) -> Result { + fn or_unexpected(self) -> Result { self.ok_or(Error::from(OPTION_NONE_ERROR)).or_unexpected() } - fn or_unexpected_while(self, prefix: D) -> Result { + fn or_unexpected_while(self, prefix: D) -> Result { self.ok_or(Error::from(OPTION_NONE_ERROR)) .or_unexpected_while(prefix) } - fn map_unexpected_while(self, f: F) -> Result + fn map_unexpected_while(self, f: F) -> Result where F: FnOnce() -> D, D: fmt::Display,