model mutability

This commit is contained in:
Cyryl Płotnicki 2019-09-01 11:05:22 +01:00
parent 0229d6c9fd
commit d2e5c480a9
3 changed files with 42 additions and 30 deletions

View file

@ -7,15 +7,15 @@ use crate::repository::Repository;
pub struct Engine<'a> {
source_path: &'a Path,
repository: &'a Repository<'a>,
repository: &'a mut Repository<'a>,
}
impl<'a> Engine<'a> {
pub fn new(source_path: &'a Path, repository: &'a Repository) -> Self {
pub fn new(source_path: &'a Path, repository: &'a mut Repository<'a>) -> Self {
Engine { source_path, repository }
}
pub fn backup(&self) -> Result<(), BakareError> {
pub fn backup(&mut self) -> Result<(), BakareError> {
let walker = WalkDir::new(self.source_path);
for maybe_entry in walker {
let entry = maybe_entry?;

View file

@ -25,7 +25,7 @@ pub struct Repository<'a> {
#[derive(Clone)]
pub struct RepositoryItem<'a> {
version: ItemVersion<'a>,
relative_path: Rc<Path>,
relative_path: Box<Path>,
}
pub struct RepositoryIterator<'a> {
@ -66,7 +66,7 @@ impl<'a> Repository<'a> {
.into_iter()
.map(|p| RepositoryItem {
version: ItemVersion(""),
relative_path: Rc::from(p.path()),
relative_path: Box::from(p.path()),
})
.collect();
@ -89,25 +89,23 @@ impl<'a> Repository<'a> {
}
}
pub fn store(&self, source_path: &Path) -> Result<(), BakareError> {
// get file id -> contents hash + original path + time of taking notes
// get storage path for File
// store file contents
// remember File
pub fn store(&mut self, source_path: &Path) -> Result<(), BakareError> {
let destination_path = self.path.join(source_path);
if source_path.is_dir() {
fs::create_dir(destination_path.clone())?;
}
if source_path.is_file() {}
// TODO create new version, remember source_path
if source_path.is_file() {
// TODO: copy file
self.index.items.push(RepositoryItem {
version: ItemVersion(""),
relative_path: destination_path.into_boxed_path(),
});
}
Ok(())
}
pub fn item(&self, path: &Path) -> Option<&RepositoryItem> {
None
self.index.items.iter().find(|i| *i.relative_path == *path)
}
pub fn newest_version_for(&self, item: RepositoryItem) -> ItemVersion {

View file

@ -26,24 +26,34 @@ fn restore_backed_up_files() -> Result<(), BakareError> {
fn restore_older_version_of_file() -> Result<(), BakareError> {
let source = TempSource::new()?;
let repository_path = tempdir()?.into_path();
let repository = Repository::open(repository_path.as_path())?;
let backup_engine = backup::Engine::new(source.path(), &repository);
let restore_repository = Repository::open(repository_path.as_path())?;
let relative_path_text = "some path";
let file_path = source.file_path(relative_path_text);
let new_contents = "totally new contents";
let restore_target = tempdir()?;
let restore_engine = restore::Engine::new(&repository, &restore_target.path());
let restore_engine = restore::Engine::new(&restore_repository, &restore_target.path());
let old_contents = "some old contents";
source.write_text_to_file(relative_path_text, old_contents)?;
backup_engine.backup()?;
let file_id = repository.item(&file_path);
{
let mut backup_repository = Repository::open(repository_path.as_path())?;
let mut backup_engine = backup::Engine::new(source.path(), &mut backup_repository);
source.write_text_to_file(relative_path_text, old_contents)?;
backup_engine.backup()?;
}
let backup_repository = Repository::open(repository_path.as_path())?;
let file_id = backup_repository.item(&file_path);
assert!(file_id.is_some());
let file_id = file_id.unwrap();
let old_version = file_id.version();
source.write_text_to_file(relative_path_text, new_contents)?;
backup_engine.backup()?;
{
let mut backup_repository = Repository::open(repository_path.as_path())?;
let mut backup_engine = backup::Engine::new(source.path(), &mut backup_repository);
source.write_text_to_file(relative_path_text, new_contents)?;
backup_engine.backup()?;
}
restore_engine.restore_as_of_version(&file_id, old_version)?;
@ -60,14 +70,18 @@ fn assert_target_file_contents(target: &Path, filename: &str, expected_contents:
fn assert_same_after_restore(source_path: &Path) -> Result<(), BakareError> {
let repository_path = tempdir()?.into_path();
let repository = Repository::open(repository_path.as_path())?;
let backup_engine = backup::Engine::new(source_path, &repository);
backup_engine.backup()?;
let restore_target = tempdir()?;
let restore_engine = restore::Engine::new(&repository, &restore_target.path());
restore_engine.restore_all()?;
{
let mut backup_repository = Repository::open(repository_path.as_path())?;
let mut backup_engine = backup::Engine::new(source_path, &mut backup_repository);
backup_engine.backup()?;
}
{
let mut restore_repository = Repository::open(repository_path.as_path())?;
let restore_engine = restore::Engine::new(&mut restore_repository, &restore_target.path());
restore_engine.restore_all()?;
}
let are_source_and_target_different = is_different(source_path, &restore_target.path()).unwrap();
assert!(!are_source_and_target_different);
Ok(())