feat: further use State abstraction

export-backup
Jef Roosens 2023-07-07 18:06:15 +02:00
parent 4ec336eb86
commit 80b814bcff
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 25 additions and 22 deletions

View File

@ -1,25 +1,24 @@
use super::State;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt; use std::fmt;
use std::path::PathBuf;
/// Represents the changes relative to the previous backup /// Represents the changes relative to the previous backup
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Delta { pub struct Delta {
/// What files were added/modified in each part of the tarball. /// What files were added/modified in each part of the tarball.
pub added: HashMap<PathBuf, HashSet<PathBuf>>, pub added: State,
/// What files were removed in this backup, in comparison to the previous backup. For full /// 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. /// 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 /// The map stores a separate list for each top-level directory, as the contents of these
/// directories can come for different source directories. /// directories can come for different source directories.
pub removed: HashMap<PathBuf, HashSet<PathBuf>>, pub removed: State,
} }
impl Delta { impl Delta {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
added: HashMap::new(), added: Default::default(),
removed: HashMap::new(), removed: Default::default(),
} }
} }
@ -111,23 +110,20 @@ impl Delta {
out out
} }
/// Given a list of deltas, return a new list of the same length containing each delta, but pub fn contributions(deltas: &[Self]) -> Vec<State> {
/// with all its successive deltas subtracted from it. This resulting list would produce the let mut contributions: Vec<State> = Vec::with_capacity(deltas.len());
/// same delta if merged, but with each delta only representing the parts of the final state // contributions[deltas.len() - 1] = deltas[deltas.len() - 1].added.clone();
/// that it is the sole contributor to.
pub fn disjunct(deltas: &Vec<Self>) -> Vec<Delta> {
let mut contributions = Vec::with_capacity(deltas.len());
for (i, delta) in deltas.iter().enumerate() { // for (i, delta) in deltas.iter().enumerate() {
// The base case for the contributions is every added file // // The base case for the contributions is every added file
let mut contribution = delta.clone(); // let mut contribution = delta.clone();
for other_delta in &deltas[i + 1..] { // for other_delta in &deltas[i + 1..] {
contribution = contribution.difference(other_delta); // contribution = contribution.difference(other_delta);
} // }
contributions.push(contribution); // contributions.push(contribution);
} // }
contributions contributions
} }

View File

@ -1,12 +1,13 @@
use crate::backup::Delta; use crate::backup::Delta;
use serde::{Deserialize, Serialize};
use std::borrow::Borrow; use std::borrow::Borrow;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::ops::Deref; use std::ops::{Deref, DerefMut};
use std::path::PathBuf; use std::path::PathBuf;
/// Struct that represents a current state for a backup. This struct acts as a smart pointer around /// Struct that represents a current state for a backup. This struct acts as a smart pointer around
/// a HashMap. /// a HashMap.
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct State(HashMap<PathBuf, HashSet<PathBuf>>); pub struct State(HashMap<PathBuf, HashSet<PathBuf>>);
impl State { 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 { impl Default for State {
fn default() -> Self { fn default() -> Self {
Self::new() Self::new()