use std::error::Error;
use std::fmt::Debug;
use notify_rust::{Notification, Urgency};
use serde::{Serialize, Deserialize};
use crate::destination::{MessageDestination, SerializableDestination};
use crate::message::{Level, Message};
#[derive(Debug, Serialize, Deserialize)]
pub struct DesktopNotificationReceiver {}
impl MessageDestination for DesktopNotificationReceiver {
fn send(&self, message: &Message) -> Result<(), Box<dyn Error>> {
let mut notification = Notification::new();
let mut title = String::new();
if let Some(message_title) = message.get_title() {
title.push_str(message_title)
}
if let Some(component) = message.get_component() {
if !title.is_empty() {
title.push_str(" - ");
}
title.push_str(&format!("{}", component));
}
notification.summary(&title)
.auto_icon();
let body = format!("{}\nFrom: {}", message.get_message_detail().raw(), message.get_author());
notification.body(&body);
#[cfg(all(unix, not(target_os = "macos")))]
{
let urgency = match message.get_level() {
Level::Info => Urgency::Normal,
Level::Warn => Urgency::Normal,
Level::Error => Urgency::Critical,
Level::SelfError => Urgency::Critical,
};
notification.urgency(urgency);
let icon = match message.get_level() {
Level::Info => "dialog-information",
Level::Warn => "dialog-warning",
Level::Error => "dialog-error",
Level::SelfError => "dialog-error",
};
notification.icon(icon);
}
notification.show()?;
Ok(())
}
}
#[typetag::serde(name = "Desktop")]
impl SerializableDestination for DesktopNotificationReceiver {
fn as_message_destination(&self) -> &dyn MessageDestination {
self
}
}