introduce iterator type

This commit is contained in:
Cyryl Płotnicki 2019-01-26 20:11:11 +00:00
parent 916b537e17
commit 1d57ce9a89
5 changed files with 17 additions and 10 deletions

View file

@ -9,7 +9,6 @@ use walkdir::WalkDir;
use crate::error::BakareError;
use crate::repository::Repository;
use crate::RepositoryRelativePath;
use crate::Version;
pub struct Engine<'a> {

View file

@ -13,8 +13,6 @@ pub mod source;
pub mod repository;
pub struct Version(String);
struct RepositoryRelativePath {}
struct Index<'a> {
versions: HashMap<&'a RepositoryRelativePath, &'a Version>,
struct Index {
}

View file

@ -7,19 +7,23 @@ use walkdir::DirEntry;
use crate::error::BakareError;
use crate::Version;
use crate::Index;
/// represents a place where backup is stored an can be restored from.
/// right now only on-disk directory storage is supported
pub struct Repository<'a> {
/// absolute path to where the repository is stored on disk
path: &'a Path,
index: Index
}
pub struct RepositoryItem {
version: Version
}
impl<'a> Iterator for &Repository<'a> {
pub struct RepositoryIterator;
impl<'a> Iterator for RepositoryIterator {
type Item = RepositoryItem;
fn next(&mut self) -> Option<Self::Item> {
@ -34,8 +38,14 @@ impl RepositoryItem {
}
impl<'a> Repository<'a> {
pub fn new(path: &Path) -> Result<Repository, BakareError> {
Ok(Repository { path })
pub fn open(path: &Path) -> Result<Repository, BakareError> {
// TODO open index from file
Ok(Repository { path, index: Index {} })
}
pub fn iter(&self) -> RepositoryIterator {
unimplemented!()
}
pub fn store(&self, source_path: &Path) -> Result<(), BakareError> {

View file

@ -22,7 +22,7 @@ impl<'a> Engine<'a> {
}
pub fn restore_all(&self) -> Result<(), BakareError> {
for ref item in self.repository {
for ref item in self.repository.iter() {
self.restore(item)?;
}
Ok(())

View file

@ -26,7 +26,7 @@ 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::new(repository_path.as_path())?;
let repository = Repository::open(repository_path.as_path())?;
let backup_engine = backup::Engine::new(source.path(), &repository);
let relative_path_text = "some path";
let file_path = source.file_path(relative_path_text);
@ -58,7 +58,7 @@ 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::new(repository_path.as_path())?;
let repository = Repository::open(repository_path.as_path())?;
let backup_engine = backup::Engine::new(source_path, &repository);
backup_engine.backup()?;