translations: Make the code cleaner

This commit is contained in:
Dorota Czaplejewicz
2019-12-11 15:20:29 +00:00
parent a93f3c55e7
commit 0bfd846139
5 changed files with 108 additions and 68 deletions

View File

@ -70,12 +70,12 @@ pub enum Level {
/// Approach 2.
pub trait Warn {
type Value;
fn ok_warn(self, msg: &str) -> Option<Self::Value>;
fn or_warn(self, msg: &str) -> Option<Self::Value>;
}
impl<T, E: Error> Warn for Result<T, E> {
type Value = T;
fn ok_warn(self, msg: &str) -> Option<T> {
fn or_warn(self, msg: &str) -> Option<T> {
self.map_err(|e| {
eprintln!("{}: {}", msg, e);
e
@ -83,6 +83,16 @@ impl<T, E: Error> Warn for Result<T, E> {
}
}
impl<T> Warn for Option<T> {
type Value = T;
fn or_warn(self, msg: &str) -> Option<T> {
self.or_else(|| {
eprintln!("{}", msg);
None
})
}
}
/// A mutable handler for text warnings.
/// Approach 3.
pub trait WarningHandler {