use crate::model::AutostartEntry;
use crate::provider::AutostartProvider;
use crate::providers::{SystemdProvider, XdgAutostartProvider};
#[derive(Debug)]
pub struct ProviderError {
pub source: &'static str,
pub error: anyhow::Error,
}
#[derive(Debug, Default)]
pub struct AggregateResult {
pub entries: Vec<AutostartEntry>,
pub errors: Vec<ProviderError>,
}
pub struct Registry {
providers: Vec<Box<dyn AutostartProvider>>,
}
impl Registry {
pub fn new(providers: Vec<Box<dyn AutostartProvider>>) -> Self {
Self { providers }
}
pub fn with_defaults() -> Self {
Self::new(vec![
Box::new(XdgAutostartProvider::user()),
Box::new(XdgAutostartProvider::system()),
Box::new(SystemdProvider::user()),
Box::new(SystemdProvider::system()),
])
}
pub fn providers(&self) -> &[Box<dyn AutostartProvider>] {
&self.providers
}
pub async fn available(&self) -> Vec<&dyn AutostartProvider> {
let mut out = Vec::new();
for provider in &self.providers {
if provider.is_available().await {
out.push(provider.as_ref());
}
}
out
}
pub fn find_provider(&self, id: &str) -> Option<&dyn AutostartProvider> {
self.providers
.iter()
.map(|p| p.as_ref())
.find(|p| p.id() == id)
}
pub async fn all_entries(&self) -> AggregateResult {
let mut result = AggregateResult::default();
for provider in self.available().await {
match provider.entries().await {
Ok(entries) => result.entries.extend(entries),
Err(error) => result.errors.push(ProviderError {
source: provider.id(),
error,
}),
}
}
result
}
}
impl Default for Registry {
fn default() -> Self {
Self::with_defaults()
}
}