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 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<PathBuf, HashSet<PathBuf>>,
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<PathBuf, HashSet<PathBuf>>,
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<Self>) -> Vec<Delta> {
let mut contributions = Vec::with_capacity(deltas.len());
pub fn contributions(deltas: &[Self]) -> Vec<State> {
let mut contributions: Vec<State> = 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
}

View File

@ -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<PathBuf, HashSet<PathBuf>>);
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()