bakare/src/restore.rs

33 lines
818 B
Rust
Raw Normal View History

2018-10-04 15:29:19 +01:00
use std::path::Path;
2019-01-12 14:39:20 +00:00
2020-11-08 20:02:26 +00:00
use crate::repository::{item::RepositoryItem, Repository};
use anyhow::Result;
use anyhow::*;
2019-01-12 14:39:20 +00:00
2018-10-04 15:33:01 +01:00
pub struct Engine<'a> {
2020-12-25 16:29:00 +00:00
repository: &'a mut Repository,
2018-10-04 15:29:19 +01:00
target_path: &'a Path,
}
2018-10-04 15:33:01 +01:00
impl<'a> Engine<'a> {
2020-12-25 16:29:00 +00:00
pub fn new(repository: &'a mut Repository, target_path: &'a Path) -> Result<Self> {
2019-09-01 17:03:27 +01:00
if !target_path.is_absolute() {
return Err(anyhow!("path to store not absolute"));
2019-09-01 17:03:27 +01:00
}
Ok(Engine { repository, target_path })
2018-10-04 15:29:19 +01:00
}
pub fn restore_all(&mut self) -> Result<()> {
2019-09-23 12:18:18 +01:00
for item in self.repository.newest_items() {
2019-09-01 20:10:00 +01:00
self.restore(&item)?;
2019-01-12 14:39:20 +00:00
}
self.repository.save_index()?;
2019-01-12 14:39:20 +00:00
Ok(())
2018-10-04 15:29:19 +01:00
}
pub fn restore(&self, item: &RepositoryItem) -> Result<()> {
2019-09-01 17:03:27 +01:00
item.save(self.target_path)?;
2019-09-01 12:57:37 +01:00
Ok(())
2018-10-04 15:29:19 +01:00
}
}