use std::{error::Error, fmt::Display};
pub trait IntoResult {
fn into_result(self) -> Result<(), Box<dyn Error>>;
}
impl IntoResult for () {
fn into_result(self) -> Result<(), Box<dyn Error>> {
Ok(())
}
}
impl<T> IntoResult for Option<T> {
fn into_result(self) -> Result<(), Box<dyn Error>> {
match self {
Some(_) => Ok(()),
None => Err(Box::new(NoneError)),
}
}
}
impl<T> IntoResult for Result<T, Box<dyn Error>> {
fn into_result(self) -> Result<(), Box<dyn Error>> {
match self {
Ok(_) => Ok(()),
Err(error) => Err(error),
}
}
}
#[derive(Debug, Clone, Copy)]
struct NoneError;
impl Error for NoneError {
fn description(&self) -> &str {
"NoneError: Expected Some(...), got None."
}
}
impl Display for NoneError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.pad("NoneError: expected Some(...), got None.")
}
}