Clean things up a bit

This commit is contained in:
Marc Plano-Lesay 2025-05-02 14:23:12 +10:00
parent a91e243ecc
commit 3275f4890d
Signed by: kernald
GPG key ID: 66A41B08CC62A6CF
18 changed files with 572 additions and 249 deletions

View file

@ -0,0 +1,123 @@
use crate::app::Enableable;
use crate::notifications::notification::Notification;
use color_eyre::eyre::Result;
use log::{debug, info, warn};
/// Initialize a notification from a configuration
pub fn init_notification<C, N, F>(
config_opt: &Option<C>,
creator: F,
) -> Result<Option<Box<dyn Notification>>>
where
C: Enableable + Clone,
N: Notification + 'static,
F: FnOnce(C) -> Result<N>,
{
if let Some(config) = config_opt {
if config.is_enabled() {
info!("Initializing {}", N::name());
match creator(config.clone()) {
Ok(notification) => {
return Ok(Some(Box::new(notification)));
}
Err(e) => {
warn!("Failed to initialize {}: {}", N::name(), e);
}
}
} else {
debug!("{} is disabled", N::name());
}
} else {
debug!("No {} configuration found", N::name());
}
Ok(None)
}
#[cfg(test)]
mod tests {
use super::*;
use async_trait::async_trait;
use color_eyre::eyre::{eyre, Result};
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};
#[derive(Clone)]
struct FakeConfig {
enabled: bool,
}
impl Enableable for FakeConfig {
fn is_enabled(&self) -> bool {
self.enabled
}
}
struct FakeNotification {}
#[async_trait]
impl Notification for FakeNotification {
fn name() -> &'static str {
"FakeNotification"
}
fn get_name(&self) -> &'static str {
"FakeNotification"
}
async fn send_notification(
&self,
_action_results: &HashMap<String, crate::actions::action::ProcessedMagnets>,
_total_new_links: usize,
) -> Result<()> {
Ok(())
}
}
#[test]
fn test_init_notification_no_config() {
let config: Option<FakeConfig> = None;
let result = init_notification::<_, FakeNotification, _>(&config, |_| {
panic!("Creator should not be called when config is None");
})
.unwrap();
assert!(result.is_none());
}
#[test]
fn test_init_notification_disabled_config() {
let config = Some(FakeConfig { enabled: false });
let result = init_notification::<_, FakeNotification, _>(&config, |_| {
panic!("Creator should not be called when config is disabled");
})
.unwrap();
assert!(result.is_none());
}
#[test]
fn test_init_notification_creator_fails() {
let config = Some(FakeConfig { enabled: true });
let result =
init_notification::<_, FakeNotification, _>(&config, |_| Err(eyre!("Creator failed")))
.unwrap();
assert!(result.is_none());
}
#[test]
fn test_init_notification_success() {
let config = Some(FakeConfig { enabled: true });
let creator_called = AtomicBool::new(false);
let result = init_notification::<_, FakeNotification, _>(&config, |_| {
creator_called.store(true, Ordering::SeqCst);
Ok(FakeNotification {})
})
.unwrap();
assert!(creator_called.load(Ordering::SeqCst));
assert!(result.is_some());
}
}

View file

@ -1,2 +1,3 @@
pub mod factory;
pub mod notification;
pub mod ntfy;

View file

@ -7,7 +7,11 @@ use std::collections::HashMap;
#[async_trait]
pub trait Notification {
/// Return the name of the notification service
fn name(&self) -> &str;
fn name() -> &'static str
where
Self: Sized;
fn get_name(&self) -> &'static str;
/// Send a notification about the processing results
async fn send_notification(

View file

@ -1,7 +1,8 @@
use crate::app::Enableable;
use serde::{Deserialize, Serialize};
/// Configuration for the ntfy notification service
#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NtfyConfig {
/// Whether to enable the ntfy notification service
#[serde(default)]
@ -21,3 +22,9 @@ pub struct NtfyConfig {
/// The topic to publish notifications to
pub topic: String,
}
impl Enableable for NtfyConfig {
fn is_enabled(&self) -> bool {
self.enable
}
}

View file

@ -32,10 +32,15 @@ impl NtfyNotification {
#[async_trait]
impl Notification for NtfyNotification {
fn name(&self) -> &str {
/// Return the name of the notification service
fn name() -> &'static str {
"Ntfy"
}
fn get_name(&self) -> &'static str {
Self::name()
}
async fn send_notification(
&self,
action_results: &HashMap<String, ProcessedMagnets>,