~cyplo/bakare

bakare/src/restore.rs -rw-r--r-- 731 bytes
2c3fb1b1 — Cyryl Płotnicki Update cargo dependencies 4 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use std::path::Path;

use crate::repository::{item::RepositoryItem, Repository};
use anyhow::Result;

pub struct Engine<'a> {
    repository: &'a mut Repository,
    target_path: &'a Path,
}

impl<'a> Engine<'a> {
    pub fn new(repository: &'a mut Repository, target_path: &'a Path) -> Result<Self> {
        Ok(Engine { repository, target_path })
    }

    pub fn restore_all(&mut self) -> Result<()> {
        let newest_items = self.repository.newest_items();
        for item in newest_items {
            self.restore(&item)?;
        }
        self.repository.save_index()?;
        Ok(())
    }

    pub fn restore(&self, item: &RepositoryItem) -> Result<()> {
        item.save(self.target_path)?;
        Ok(())
    }
}