From e2b89e85f7980672f2455f7d67a27dd0cab09ef4 Mon Sep 17 00:00:00 2001 From: Dorota Czaplejewicz Date: Mon, 16 Dec 2019 19:15:40 +0000 Subject: [PATCH] logging: Move all facilities to one file --- src/data.rs | 23 ++++------------------- src/lib.rs | 1 + src/logging.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ src/style.rs | 2 +- src/tests.rs | 2 +- src/util.rs | 21 --------------------- 6 files changed, 50 insertions(+), 42 deletions(-) create mode 100644 src/logging.rs diff --git a/src/data.rs b/src/data.rs index 54636f57..a36a4823 100644 --- a/src/data.rs +++ b/src/data.rs @@ -19,17 +19,17 @@ use ::keyboard::{ }; use ::layout; use ::layout::ArrangementKind; +use ::logging::PrintWarnings; use ::resources; use ::util::c::as_str; use ::util::hash_map_map; use ::xdg; // traits, derives +use serde::Deserialize; use std::io::BufReader; use std::iter::FromIterator; -use serde::Deserialize; -use util::WarningHandler; - +use ::logging::WarningHandler; /// Gathers stuff defined in C or called by C pub mod c { @@ -152,14 +152,6 @@ fn list_layout_sources( ret } -struct PrintWarnings; - -impl WarningHandler for PrintWarnings { - fn handle(&mut self, warning: &str) { - println!("{}", warning); - } -} - fn load_layout_data(source: DataSource) -> Result<::layout::LayoutData, LoadError> { @@ -672,14 +664,7 @@ mod tests { use super::*; use std::error::Error as ErrorTrait; - - struct PanicWarn; - - impl WarningHandler for PanicWarn { - fn handle(&mut self, warning: &str) { - panic!("{}", warning); - } - } + use ::logging::PanicWarn; #[test] fn test_parse_path() { diff --git a/src/lib.rs b/src/lib.rs index d7bf622d..5679656c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -24,6 +24,7 @@ mod keyboard; mod layout; mod locale; mod locale_config; +mod logging; mod outputs; mod popover; mod resources; diff --git a/src/logging.rs b/src/logging.rs new file mode 100644 index 00000000..79244d77 --- /dev/null +++ b/src/logging.rs @@ -0,0 +1,43 @@ +/*! Logging library */ + +use std::error::Error; + +/// Sugar for logging errors in results +pub trait Warn { + type Value; + fn ok_warn(self, msg: &str) -> Option; +} + +impl Warn for Result { + type Value = T; + fn ok_warn(self, msg: &str) -> Option { + self.map_err(|e| { + eprintln!("{}: {}", msg, e); + e + }).ok() + } +} + +pub trait WarningHandler { + /// Handle a warning + fn handle(&mut self, warning: &str); +} + +/// Prints warnings to stderr +pub struct PrintWarnings; + +impl WarningHandler for PrintWarnings { + fn handle(&mut self, warning: &str) { + eprintln!("{}", warning); + } +} + +/// Warning handler that will panic at any warning. +/// Don't use except in tests +pub struct PanicWarn; + +impl WarningHandler for PanicWarn { + fn handle(&mut self, warning: &str) { + panic!("{}", warning); + } +} diff --git a/src/style.rs b/src/style.rs index b8f147a7..b12e756c 100644 --- a/src/style.rs +++ b/src/style.rs @@ -21,7 +21,7 @@ use std::env; use glib::object::ObjectExt; -use util::Warn; +use logging::Warn; /// Gathers stuff defined in C or called by C pub mod c { diff --git a/src/tests.rs b/src/tests.rs index d243eab4..3de54ccf 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -3,7 +3,7 @@ use ::data::Layout; use xkbcommon::xkb; -use ::util::WarningHandler; +use ::logging::WarningHandler; pub struct CountAndPrint(u32); diff --git a/src/util.rs b/src/util.rs index 8bd156cc..1553fd19 100644 --- a/src/util.rs +++ b/src/util.rs @@ -189,27 +189,6 @@ impl Borrow> for Pointer { } } -/// Sugar for logging errors in results -pub trait Warn { - type Value; - fn ok_warn(self, msg: &str) -> Option; -} - -impl Warn for Result { - type Value = T; - fn ok_warn(self, msg: &str) -> Option { - self.map_err(|e| { - eprintln!("{}: {}", msg, e); - e - }).ok() - } -} - -pub trait WarningHandler { - /// Handle a warning - fn handle(&mut self, warning: &str); -} - #[cfg(test)] mod tests { use super::*;