1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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)
}
}