use std::io::ErrorKind;
use crate::exit::ExitCode;
pub enum Settings {
Ignore(ErrorKind),
IgnoreAny(Vec<ErrorKind>),
}
pub trait ToExitCode<T> {
fn to_exit_code(self) -> Result<T, ExitCode>;
fn to_exit_code_with(self, _: Settings) -> Result<T, ExitCode>;
}
impl<T> ToExitCode<T> for Result<T, ::std::io::Error> {
fn to_exit_code(self) -> Result<T, ExitCode> {
self.to_exit_code_with(Settings::Ignore(ErrorKind::BrokenPipe))
}
fn to_exit_code_with(self, settings: Settings) -> Result<T, ExitCode> {
self.map_err(move |e| match settings {
Settings::Ignore(kind) => if e.kind() == kind {
0
} else {
1
},
Settings::IgnoreAny(v) => if v.iter().any(|el| e.kind() == *el) {
0
} else {
1
},
})
.map_err(ExitCode::from)
}
}