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::error::BakareError;
use crate::repository::Repository; use crate::repository::Repository;
use crate::RepositoryRelativePath;
use crate::Version; use crate::Version;
pub struct Engine<'a> { pub struct Engine<'a> {

View file

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

View file

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

View file

@ -26,7 +26,7 @@ fn restore_backed_up_files() -> Result<(), BakareError> {
fn restore_older_version_of_file() -> Result<(), BakareError> { fn restore_older_version_of_file() -> Result<(), BakareError> {
let source = TempSource::new()?; let source = TempSource::new()?;
let repository_path = tempdir()?.into_path(); 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 backup_engine = backup::Engine::new(source.path(), &repository);
let relative_path_text = "some path"; let relative_path_text = "some path";
let file_path = source.file_path(relative_path_text); 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> { fn assert_same_after_restore(source_path: &Path) -> Result<(), BakareError> {
let repository_path = tempdir()?.into_path(); 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 backup_engine = backup::Engine::new(source_path, &repository);
backup_engine.backup()?; backup_engine.backup()?;