use crate::{clear_notification, notification_directory, set_notification};
use std::{fs, io, path};
pub struct Namespace {
prefix: String,
}
impl Namespace {
pub fn new(prefix: String) -> Self {
Namespace { prefix }
}
fn namespaced(&self, id: String) -> String {
format!("{}-{}", self.prefix, id)
}
pub fn set_notification(
&self,
id: String,
text: String,
command: String,
watched_file: Option<&path::Path>,
) -> io::Result<()> {
set_notification(self.namespaced(id), text, command, watched_file)
}
pub fn clear_notification(&self, id: String) -> io::Result<()> {
clear_notification(self.namespaced(id))
}
pub fn cleanup(self) -> io::Result<()> {
let Self { prefix } = self;
fs::read_dir(notification_directory())?
.filter_map(|f| Some(f.ok()?.path()))
.filter_map(|f| {
if f.file_name()?.to_str()?.starts_with(&prefix) {
Some(f)
} else {
None
}
})
.fold(Ok(()), |e, f| {
fs::remove_file(f)?;
e
})
}
}