|
|
|
@ -3,6 +3,7 @@ use std::{
|
|
|
|
|
hash::Hash,
|
|
|
|
|
collections::{HashSet, HashMap},
|
|
|
|
|
mem,
|
|
|
|
|
fmt,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
pub trait DedupFullExt
|
|
|
|
@ -37,6 +38,70 @@ where T: Hash + Default + Eq
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub trait ErrorLogForgetExt<T, E>: Sized
|
|
|
|
|
{
|
|
|
|
|
fn log_and_forget(self, mode: &log::Mode, level: log::Level) -> Option<T>;
|
|
|
|
|
}
|
|
|
|
|
impl<T,E> ErrorLogForgetExt<T,E> for Result<T,E>
|
|
|
|
|
where E: fmt::Display,
|
|
|
|
|
{
|
|
|
|
|
fn log_and_forget(self, mode: &log::Mode, level: log::Level) -> Option<T>
|
|
|
|
|
{
|
|
|
|
|
match self {
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log!(mode.level(level), "{}", &e);
|
|
|
|
|
if let log::Level::Fatal = level {
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
None
|
|
|
|
|
},
|
|
|
|
|
Ok(v) => Some(v),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
pub trait ErrorLogExt: Sized
|
|
|
|
|
{
|
|
|
|
|
fn into_log(self, mode: &log::Mode, level: log::Level) -> Self;
|
|
|
|
|
fn into_fatal(self, mode: Option<log::Mode>) -> Self
|
|
|
|
|
{
|
|
|
|
|
self.into_log(&mode.unwrap_or(log::Mode::Verbose), log::Level::Fatal)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T,E> ErrorLogExt for Result<T,E>
|
|
|
|
|
where E: fmt::Display
|
|
|
|
|
{
|
|
|
|
|
fn into_log(self, mode: &log::Mode, level: log::Level) -> Self
|
|
|
|
|
{
|
|
|
|
|
match self {
|
|
|
|
|
Err(e) => {
|
|
|
|
|
log!(mode.level(level), "{}", &e);
|
|
|
|
|
if let log::Level::Fatal = level {
|
|
|
|
|
std::process::exit(1);
|
|
|
|
|
}
|
|
|
|
|
Err(e)
|
|
|
|
|
},
|
|
|
|
|
Ok(v) => Ok(v),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub trait ErrorExt<T>
|
|
|
|
|
{
|
|
|
|
|
fn into_string(self) -> Result<T, String>;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T,E> ErrorExt<T> for Result<T,E>
|
|
|
|
|
where E: fmt::Display
|
|
|
|
|
{
|
|
|
|
|
fn into_string(self) -> Result<T, String>
|
|
|
|
|
{
|
|
|
|
|
match self {
|
|
|
|
|
Ok(v) => Ok(v),
|
|
|
|
|
Err(e) => Err(format!("{}", e)),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod test
|
|
|
|
|