/*
* Copyright (C) 2025 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under GPL-3.0-or-later, see NOTICE and LICENSE for
* more information.
*/
use std::{collections::HashMap, sync::Arc};
use crate::{
config::{Config, NotifierConfig},
service::{self, Service},
};
static USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"));
#[derive(Clone)]
pub struct State {
_client: reqwest::Client,
pub services: Arc<HashMap<String, Box<dyn Service + Send + Sync>>>,
pub notifiers: HashMap<String, NotifierConfig>,
}
impl State {
pub fn from_config(config: &Config) -> Result<Self, Box<dyn std::error::Error>> {
let client: reqwest::Client = reqwest::ClientBuilder::new()
.user_agent(USER_AGENT)
.build()?;
let mut services: HashMap<String, Box<dyn Service + Send + Sync>> = HashMap::new();
for (id, service_config) in &config.services {
let service: Box<dyn Service + Send + Sync> =
service::build(client.clone(), &*(*service_config));
services.insert(id.clone(), service);
}
Ok(Self {
_client: client,
services: Arc::new(services),
notifiers: config.notifiers.clone(),
})
}
}