From 80b814bcffbb65e674ef3b94b418635f2c9bfded Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Fri, 7 Jul 2023 18:06:15 +0200 Subject: [PATCH] feat: further use State abstraction --- src/backup/delta.rs | 36 ++++++++++++++++-------------------- src/backup/state.rs | 11 +++++++++-- 2 files changed, 25 insertions(+), 22 deletions(-) diff --git a/src/backup/delta.rs b/src/backup/delta.rs index 61ceb1c..f32f263 100644 --- a/src/backup/delta.rs +++ b/src/backup/delta.rs @@ -1,25 +1,24 @@ +use super::State; use serde::{Deserialize, Serialize}; -use std::collections::{HashMap, HashSet}; use std::fmt; -use std::path::PathBuf; /// Represents the changes relative to the previous backup #[derive(Debug, Serialize, Deserialize, Clone)] pub struct Delta { /// What files were added/modified in each part of the tarball. - pub added: HashMap>, + pub added: State, /// What files were removed in this backup, in comparison to the previous backup. For full /// backups, this will always be empty, as they do not consider previous backups. /// The map stores a separate list for each top-level directory, as the contents of these /// directories can come for different source directories. - pub removed: HashMap>, + pub removed: State, } impl Delta { pub fn new() -> Self { Self { - added: HashMap::new(), - removed: HashMap::new(), + added: Default::default(), + removed: Default::default(), } } @@ -111,23 +110,20 @@ impl Delta { out } - /// Given a list of deltas, return a new list of the same length containing each delta, but - /// with all its successive deltas subtracted from it. This resulting list would produce the - /// same delta if merged, but with each delta only representing the parts of the final state - /// that it is the sole contributor to. - pub fn disjunct(deltas: &Vec) -> Vec { - let mut contributions = Vec::with_capacity(deltas.len()); + pub fn contributions(deltas: &[Self]) -> Vec { + let mut contributions: Vec = Vec::with_capacity(deltas.len()); + // contributions[deltas.len() - 1] = deltas[deltas.len() - 1].added.clone(); - for (i, delta) in deltas.iter().enumerate() { - // The base case for the contributions is every added file - let mut contribution = delta.clone(); + // for (i, delta) in deltas.iter().enumerate() { + // // The base case for the contributions is every added file + // let mut contribution = delta.clone(); - for other_delta in &deltas[i + 1..] { - contribution = contribution.difference(other_delta); - } + // for other_delta in &deltas[i + 1..] { + // contribution = contribution.difference(other_delta); + // } - contributions.push(contribution); - } + // contributions.push(contribution); + // } contributions } diff --git a/src/backup/state.rs b/src/backup/state.rs index fa60738..dcd9c41 100644 --- a/src/backup/state.rs +++ b/src/backup/state.rs @@ -1,12 +1,13 @@ use crate::backup::Delta; +use serde::{Deserialize, Serialize}; use std::borrow::Borrow; use std::collections::{HashMap, HashSet}; -use std::ops::Deref; +use std::ops::{Deref, DerefMut}; use std::path::PathBuf; /// Struct that represents a current state for a backup. This struct acts as a smart pointer around /// a HashMap. -#[derive(Debug, Clone, PartialEq, Eq)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] pub struct State(HashMap>); impl State { @@ -63,6 +64,12 @@ impl Deref for State { } } +impl DerefMut for State { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + impl Default for State { fn default() -> Self { Self::new()