use std::collections::HashMap;
use thiserror::Error;
use crate::checkers::history::last_paths::{Error as LastPathsError, LastPaths};
use crate::checkers::history::operation::{Error as OperationError, Operation};
use crate::hoard::{Direction, Hoard};
use crate::newtypes::HoardName;
use crate::paths::HoardPath;
pub mod history;
#[async_trait::async_trait(? Send)]
pub trait Checker: Sized + Unpin {
type Error: std::error::Error;
async fn new(
hoard_root: &HoardPath,
hoard_name: &HoardName,
hoard: &Hoard,
direction: Direction,
) -> Result<Self, Self::Error>;
async fn check(&mut self) -> Result<(), Self::Error>;
async fn commit_to_disk(self) -> Result<(), Self::Error>;
}
#[derive(Debug, Error)]
#[allow(variant_size_differences)]
pub enum Error {
#[error("error while comparing previous run to current run: {0}")]
LastPaths(#[from] LastPathsError),
#[error("error while checking against recent remote operations: {0}")]
Operation(#[from] OperationError),
}
#[derive(Debug, Clone, PartialEq)]
pub struct Checkers {
last_paths: HashMap<HoardName, LastPaths>,
operations: HashMap<HoardName, Operation>,
}
impl Checkers {
#[allow(single_use_lifetimes)]
#[tracing::instrument(level = "debug", name = "checkers_new", skip(hoards))]
pub(crate) async fn new<'a>(
hoards_root: &HoardPath,
hoards: impl IntoIterator<Item = (&'a HoardName, &'a Hoard)>,
direction: Direction,
) -> Result<Self, Error> {
let mut last_paths = HashMap::new();
let mut operations = HashMap::new();
for (name, hoard) in hoards {
tracing::debug!(%name, ?hoard, "processing hoard");
let lp = LastPaths::new(hoards_root, name, hoard, direction).await?;
let op = Operation::new(hoards_root, name, hoard, direction).await?;
last_paths.insert(name.clone(), lp);
operations.insert(name.clone(), op);
}
Ok(Self {
last_paths,
operations,
})
}
#[tracing::instrument(level = "debug", name = "checkers_check", skip_all)]
pub(crate) async fn check(&mut self) -> Result<(), Error> {
for last_path in &mut self.last_paths.values_mut() {
last_path.check().await?;
}
for operation in self.operations.values_mut() {
operation.check().await?;
}
Ok(())
}
#[tracing::instrument(level = "debug", name = "checkers_commit", skip_all)]
pub(crate) async fn commit_to_disk(self) -> Result<(), Error> {
let Self {
last_paths,
operations,
..
} = self;
for (_, last_path) in last_paths {
last_path.commit_to_disk().await?;
}
for (_, operation) in operations {
operation.commit_to_disk().await?;
}
Ok(())
}
pub(crate) fn get_operation_for(&self, hoard_name: &HoardName) -> Option<&Operation> {
self.operations.get(hoard_name)
}
}