introduce unexpected::Result
This commit is contained in:
parent
af1dc183ec
commit
811aef209a
@ -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<T> = result::Result<T, Error>;
|
||||
|
||||
impl Error {
|
||||
fn from_displays<D1, D2, D3>(prefix: Option<D1>, body: &D2, source: Option<D3>) -> Error
|
||||
where
|
||||
@ -55,24 +57,24 @@ impl error::Error for Error {}
|
||||
|
||||
pub trait Mappable<T> {
|
||||
/// or_unexpected returns an Err(Error) wrapping self's Err, or the Ok value of self.
|
||||
fn or_unexpected(self) -> Result<T, Error>;
|
||||
fn or_unexpected(self) -> Result<T>;
|
||||
|
||||
/// 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<D: fmt::Display>(self, prefix: D) -> Result<T, Error>;
|
||||
fn or_unexpected_while<D: fmt::Display>(self, prefix: D) -> Result<T>;
|
||||
|
||||
/// map_unexpected_while is like or_unexpected_while, but uses a closure to produce the error
|
||||
/// prefix.
|
||||
fn map_unexpected_while<F, D>(self, f: F) -> Result<T, Error>
|
||||
fn map_unexpected_while<F, D>(self, f: F) -> Result<T>
|
||||
where
|
||||
F: FnOnce() -> D,
|
||||
D: fmt::Display;
|
||||
}
|
||||
|
||||
fn map_unexpected_maybe_while<T, E, F, D>(
|
||||
res: Result<T, E>,
|
||||
res: result::Result<T, E>,
|
||||
prefix_fn: Option<F>,
|
||||
) -> Result<T, Error>
|
||||
) -> Result<T>
|
||||
where
|
||||
E: error::Error,
|
||||
F: FnOnce() -> D,
|
||||
@ -84,17 +86,17 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, E: error::Error> Mappable<T> for Result<T, E> {
|
||||
fn or_unexpected(self) -> Result<T, Error> {
|
||||
impl<T, E: error::Error> Mappable<T> for result::Result<T, E> {
|
||||
fn or_unexpected(self) -> Result<T> {
|
||||
let no_fn = None::<Box<dyn FnOnce() -> Box<dyn fmt::Display>>>; // lol, good job rust
|
||||
map_unexpected_maybe_while(self, no_fn)
|
||||
}
|
||||
|
||||
fn or_unexpected_while<D: fmt::Display>(self, prefix: D) -> Result<T, Error> {
|
||||
fn or_unexpected_while<D: fmt::Display>(self, prefix: D) -> Result<T> {
|
||||
map_unexpected_maybe_while(self, Some(|| prefix))
|
||||
}
|
||||
|
||||
fn map_unexpected_while<F, D>(self, f: F) -> Result<T, Error>
|
||||
fn map_unexpected_while<F, D>(self, f: F) -> Result<T>
|
||||
where
|
||||
F: FnOnce() -> D,
|
||||
D: fmt::Display,
|
||||
@ -106,16 +108,16 @@ impl<T, E: error::Error> Mappable<T> for Result<T, E> {
|
||||
static OPTION_NONE_ERROR: &str = "expected Some but got None";
|
||||
|
||||
impl<T> Mappable<T> for Option<T> {
|
||||
fn or_unexpected(self) -> Result<T, Error> {
|
||||
fn or_unexpected(self) -> Result<T> {
|
||||
self.ok_or(Error::from(OPTION_NONE_ERROR)).or_unexpected()
|
||||
}
|
||||
|
||||
fn or_unexpected_while<D: fmt::Display>(self, prefix: D) -> Result<T, Error> {
|
||||
fn or_unexpected_while<D: fmt::Display>(self, prefix: D) -> Result<T> {
|
||||
self.ok_or(Error::from(OPTION_NONE_ERROR))
|
||||
.or_unexpected_while(prefix)
|
||||
}
|
||||
|
||||
fn map_unexpected_while<F, D>(self, f: F) -> Result<T, Error>
|
||||
fn map_unexpected_while<F, D>(self, f: F) -> Result<T>
|
||||
where
|
||||
F: FnOnce() -> D,
|
||||
D: fmt::Display,
|
||||
|
Loading…
Reference in New Issue
Block a user