diff --git a/common/src/lib.rs b/common/src/lib.rs index ba2d0f1..09a71a2 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -12,6 +12,11 @@ pub mod config { use xdg::BaseDirectories; use std::fs::{File, metadata}; + use std::str::FromStr; + use std::fmt::Debug; + use std::fmt::Display; + use std::default::Default; + pub struct Config { config_path: Option, config: Ini @@ -39,10 +44,10 @@ pub mod config { Config { config, config_path } } - pub fn get(&mut self, section: &str, key: &str) -> Option + pub fn get(&mut self, section: &str, key: &str) -> Option where - T: std::str::FromStr, - ::Err: std::fmt::Debug + T: FromStr, + ::Err: Debug { self.config.get(section, key).map(|s: String| { s.parse().unwrap() }).or_else(|| { self.config.set(section, key, None); @@ -53,9 +58,9 @@ pub mod config { pub fn get_default(&mut self, section: &str, key: &str, default: T) -> T where - T: std::str::FromStr, - T: std::fmt::Display, - ::Err: std::fmt::Debug + T: FromStr, + T: Display, + ::Err: Debug { let val: Option = self.get(section, key); @@ -65,12 +70,23 @@ pub mod config { default }) } + + pub fn get_default_from_trait(&mut self, section: &str, key: &str) -> T + where + T: FromStr, + T: Display, + T: Default, + ::Err: Debug + { + self.get_default(section, key, T::default()) + } } } pub mod notify { use libnotify::Notification; pub use libnotify::Urgency; use crate::config::Config; + use std::default::Default; fn init_if_not_already() { if ! libnotify::is_initted() { @@ -89,6 +105,12 @@ pub mod notify { Progress(f32, OSDProgressText) } + impl Default for OSDContents { + fn default() -> OSDContents { + OSDContents::Simple(None) + } + } + pub struct OSD { pub title: Option, @@ -129,7 +151,7 @@ pub mod notify { return OSD { title: None, icon: None, - contents: OSDContents::Simple(None), + contents: OSDContents::default(), urgency: Urgency::Normal, length, full, empty, start, end, notification