use crate::{
FsoType,
InvalidTendrilError,
RawTendril,
TendrilActionError,
TendrilActionSuccess,
};
use std::marker::PhantomData;
use std::path::PathBuf;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct TendrilReport<T: TendrilLog> {
pub raw_tendril: RawTendril,
pub log: Result<T, InvalidTendrilError>,
}
pub trait TendrilLog {
fn local_type(&self) -> &Option<FsoType>;
fn remote_type(&self) -> &Option<FsoType>;
fn resolved_path(&self) -> &PathBuf;
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ActionLog {
local_type: Option<FsoType>,
remote_type: Option<FsoType>,
resolved_path: PathBuf,
pub result: Result<TendrilActionSuccess, TendrilActionError>,
}
impl ActionLog {
pub fn new(
local_type: Option<FsoType>,
remote_type: Option<FsoType>,
resolved_path: PathBuf,
result: Result<TendrilActionSuccess, TendrilActionError>,
) -> ActionLog {
ActionLog { local_type, remote_type, resolved_path, result }
}
}
impl TendrilLog for ActionLog {
fn local_type(&self) -> &Option<FsoType> {
&self.local_type
}
fn remote_type(&self) -> &Option<FsoType> {
&self.remote_type
}
fn resolved_path(&self) -> &PathBuf {
&self.resolved_path
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ListLog {
local_type: Option<FsoType>,
remote_type: Option<FsoType>,
resolved_path: PathBuf,
}
impl ListLog {
pub fn new(
local_type: Option<FsoType>,
remote_type: Option<FsoType>,
resolved_path: PathBuf,
) -> ListLog {
ListLog { local_type, remote_type, resolved_path }
}
}
impl TendrilLog for ListLog {
fn local_type(&self) -> &Option<FsoType> {
&self.local_type
}
fn remote_type(&self) -> &Option<FsoType> {
&self.remote_type
}
fn resolved_path(&self) -> &PathBuf {
&self.resolved_path
}
}
pub trait UpdateHandler<L>
where
L: TendrilLog
{
fn count(&mut self, value: i32);
fn before(&mut self, raw: RawTendril);
fn after(&mut self, report: TendrilReport<L>);
}
pub struct CallbackUpdater<A, B, C, L>
where
A: FnMut(TendrilReport<L>),
B: FnMut(RawTendril),
C: FnMut(i32),
L: TendrilLog,
{
pub count: C,
pub before: B,
pub after: A,
_marker: PhantomData<L>,
}
impl<A, B, C, L> CallbackUpdater<A, B, C, L>
where
A: FnMut(TendrilReport<L>),
B: FnMut(RawTendril),
C: FnMut(i32),
L: TendrilLog,
{
pub fn new(count: C, before: B, after: A) -> CallbackUpdater<A, B, C, L> {
CallbackUpdater {
count,
before,
after,
_marker: PhantomData,
}
}
#[cfg(test)]
pub fn default() -> CallbackUpdater<impl FnMut(TendrilReport<L>), impl FnMut(RawTendril), impl FnMut(i32), L> {
CallbackUpdater {
count: |_| {},
before: |_| {},
after: |_| {},
_marker: PhantomData,
}
}
}
impl<A, B, C, L> UpdateHandler<L> for CallbackUpdater<A, B, C, L>
where
A: FnMut(TendrilReport<L>),
B: FnMut(RawTendril),
C: FnMut(i32),
L: TendrilLog,
{
fn count(&mut self, total: i32) {
(self.count)(total)
}
fn before(&mut self, raw: RawTendril) {
(self.before)(raw)
}
fn after(&mut self, report: TendrilReport<L>) {
(self.after)(report)
}
}