ephemeris_forge/
backend.rs1use std::error::Error as StdError;
4use std::fmt;
5
6use facet::Facet;
7
8use crate::{RepoRef, Report};
9
10pub trait Backend {
15 type Config: for<'a> Facet<'a> + Sync;
17
18 type Error: StdError + Send + Sync + 'static;
20
21 type Live: Send;
23
24 type Operation: fmt::Display + Send + Sync;
26
27 fn apply(
29 &self,
30 repository: &RepoRef,
31 operation: &Self::Operation,
32 ) -> impl Future<Output = Result<(), Self::Error>> + Send;
33
34 fn plan(config: &Self::Config, live: &Self::Live) -> Planned<Self::Operation>;
36
37 fn read(
39 &self,
40 repository: &RepoRef,
41 config: &Self::Config,
42 ) -> impl Future<Output = Result<Self::Live, Self::Error>> + Send;
43
44 fn validate(config: &Self::Config) -> Result<(), Self::Error>;
50}
51
52#[derive(Clone, Debug)]
54pub struct Planned<Operation> {
55 pub operations: Vec<Operation>,
57 pub report: Report,
59}
60
61impl<Operation> Default for Planned<Operation> {
62 fn default() -> Self {
63 Self {
64 operations: Vec::new(),
65 report: Report::default(),
66 }
67 }
68}
69
70impl<Operation> Planned<Operation> {
71 pub fn extend<Resource, Items, Planner>(&mut self, planner: Planner)
76 where
77 Resource: Into<Operation>,
78 Items: IntoIterator<Item = Resource>,
79 Planner: FnOnce(&mut Report) -> Items,
80 {
81 let operations = planner(&mut self.report);
82 self.operations
83 .extend(operations.into_iter().map(Into::into));
84 }
85}