diff --git a/src/error.rs b/src/error.rs index bbef8fa..a7eb556 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,7 +18,7 @@ pub enum BakareError { #[fail(display = "corrupted repository - cannot find file")] CorruptedRepoNoFile, #[fail(display = "index loading error")] - IndexLoadingError, + IndexLoadingError(Option), } impl From for BakareError { @@ -40,7 +40,7 @@ impl From for BakareError { } impl From for BakareError { - fn from(_: serde_cbor::Error) -> Self { - BakareError::IndexLoadingError + fn from(e: serde_cbor::Error) -> Self { + BakareError::IndexLoadingError(Some(e)) } } diff --git a/src/index.rs b/src/index.rs index f7685dc..e1ea8ef 100644 --- a/src/index.rs +++ b/src/index.rs @@ -2,7 +2,9 @@ use std::path::Path; use serde::{Deserialize, Serialize}; +use crate::error::BakareError; use crate::repository_item::RepositoryItem; +use std::fs::File; #[derive(Clone, Debug, PartialOrd, PartialEq, Ord, Eq, Serialize, Deserialize)] pub struct IndexItem { @@ -27,6 +29,19 @@ impl Index { } } + pub fn load(path: &Path) -> Result { + let index_file_path = path.join("index"); + let index_file = File::open(index_file_path)?; + let index: Index = serde_cbor::from_reader(index_file)?; + Ok(index) + } + + pub fn save(&self) -> Result<(), BakareError> { + let index_file = File::create(self.index_file_path())?; + serde_cbor::to_writer(index_file, &self)?; + Ok(()) + } + pub fn index_file_path(&self) -> &Path { Path::new(&self.index_path) } diff --git a/src/repository.rs b/src/repository.rs index cd6c3e3..369c0f4 100644 --- a/src/repository.rs +++ b/src/repository.rs @@ -34,8 +34,7 @@ impl<'a> Iterator for RepositoryIterator<'a> { impl<'a> Repository<'a> { pub fn init(path: &Path) -> Result<(), BakareError> { let index = Index::new(path); - let index_file = File::create(index.index_file_path())?; - serde_cbor::to_writer(index_file, &index)?; + index.save()?; Ok(()) } @@ -44,10 +43,7 @@ impl<'a> Repository<'a> { return Err(BakareError::RepositoryPathNotAbsolute); } - let index_file_path = path.join("index"); - let index_file = File::open(index_file_path)?; - let index: Index = serde_cbor::from_reader(index_file)?; - + let index = Index::load(path)?; Ok(Repository { path, index }) } @@ -82,6 +78,7 @@ impl<'a> Repository<'a> { destination_path.strip_prefix(self.path)?, version, )); + self.index.save()?; } Ok(()) } diff --git a/src/restore.rs b/src/restore.rs index 14e5b8a..a582e31 100644 --- a/src/restore.rs +++ b/src/restore.rs @@ -25,7 +25,7 @@ impl<'a> Engine<'a> { } fn restore(&self, item: &RepositoryItem) -> Result<(), BakareError> { - println!("restoring {:#?}", item); + println!("restoring {}", item); item.save(self.target_path)?; Ok(()) }