use failure::Fallible as Result;
pub trait Notificator<T> {
fn notify(&self, item: &T) -> Result<()>;
}
pub mod default {
use std::fmt::Debug;
use std::fmt::Display;
use failure::Fallible as Result;
use notify_rust::Notification as RustNotification;
use notify_rust::NotificationUrgency;
use super::Notificator;
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug)]
pub enum Urgency {
Low,
Normal,
High
}
impl Default for Urgency {
fn default() -> Urgency {
Urgency::Normal
}
}
impl Into<NotificationUrgency> for Urgency {
fn into(self) -> NotificationUrgency {
match self {
Urgency::Low => NotificationUrgency::Low,
Urgency::Normal => NotificationUrgency::Normal,
Urgency::High => NotificationUrgency::Critical,
}
}
}
#[derive(Debug, Default, Clone)]
pub struct Notification {
pub timeout: i32,
pub message: String,
pub summary: String,
pub urgency: Urgency,
}
impl<T: Display> Notificator<T> for Notification {
fn notify(&self, item: &T) -> Result<()> {
let mut n = RustNotification::new();
n.appname("imag");
n.summary(&self.summary);
n.urgency(self.urgency.clone().into());
n.body(&format!("{}: {}", &self.message, item));
let _ = n.finalize().show(); Ok(())
}
}
#[derive(Debug, Default, Clone)]
pub struct DebugNotification(Notification);
impl From<Notification> for DebugNotification {
fn from(n: Notification) -> DebugNotification {
DebugNotification(n)
}
}
impl<T: Debug> Notificator<T> for DebugNotification {
fn notify(&self, item: &T) -> Result<()> {
let mut n = RustNotification::new();
n.appname("imag");
n.summary(&self.0.summary);
n.urgency(self.0.urgency.clone().into());
n.body(&format!("{}: {:?}", &self.0.message, item));
let _ = n.finalize().show(); Ok(())
}
}
}