use std::io;
use crate::private::Sealed;
#[allow(private_bounds)]
pub trait ResultEx<T, E>: Sealed {
fn ignore<F>(self, check: F) -> Result<Option<T>, E>
where
F: FnOnce(&E) -> bool;
fn ignore_default<F>(self, check: F) -> Result<T, E>
where
T: Default,
F: FnOnce(&E) -> bool;
fn ignore_with<F, G>(self, check: F, value: G) -> Result<T, E>
where
F: FnOnce(&E) -> bool,
G: FnOnce(&E) -> T;
}
impl<T, E> ResultEx<T, E> for Result<T, E> {
fn ignore<F>(self, check: F) -> Result<Option<T>, E>
where
F: FnOnce(&E) -> bool,
{
self.map(Some).ignore_with(check, |_| None)
}
fn ignore_default<F>(self, check: F) -> Result<T, E>
where
T: Default,
F: FnOnce(&E) -> bool,
{
self.ignore_with(check, |_| T::default())
}
fn ignore_with<F, G>(self, check: F, value: G) -> Result<T, E>
where
F: FnOnce(&E) -> bool,
G: FnOnce(&E) -> T,
{
match self {
Err(err) if check(&err) => Ok(value(&err)),
x => x,
}
}
}
pub fn io_not_found(err: &io::Error) -> bool {
err.kind() == io::ErrorKind::NotFound
}