Use std::default

This commit is contained in:
Alexander Bantyev 2020-09-16 18:30:01 +03:00
parent cee30782f6
commit 95bcf6ae14
Signed by: balsoft
GPG Key ID: E081FF12ADCB4AD5

View File

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