~cyplo/bakare

bakare/src/repository/item.rs -rw-r--r-- 2.2 KiB
2c3fb1b1 — Cyryl Płotnicki Update cargo dependencies 6 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::{repository::ItemId, version::Version};
use anyhow::Result;
use anyhow::*;
use nix::unistd::getpid;
use std::path::Path;
use std::{fmt, path::PathBuf};
use std::{
    fmt::{Display, Formatter},
    fs,
};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RepositoryItem {
    relative_path: String,
    absolute_path: PathBuf,
    original_source_path: String,
    id: ItemId,
    version: Version,
}

impl PartialOrd for RepositoryItem {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.id.partial_cmp(&other.id)
    }
}

impl RepositoryItem {
    pub fn from(original_source_path: &str, absolute_path: &Path, relative_path: &str, id: ItemId, version: Version) -> Self {
        RepositoryItem {
            relative_path: relative_path.to_string(),
            absolute_path: absolute_path.to_path_buf(),
            original_source_path: original_source_path.to_string(),
            id,
            version,
        }
    }

    pub fn save(&self, save_to: &Path) -> Result<()> {
        let original_source_path = Path::new(self.original_source_path());
        let source_path_relative = original_source_path.strip_prefix("/")?;

        let target_path = save_to.join(&source_path_relative);
        let parent = target_path
            .parent()
            .ok_or_else(|| anyhow!("cannot compute parent path for {}", &target_path.to_string_lossy()))?;
        log::debug!("[{}] saving data to {}", getpid(), target_path.to_string_lossy());
        fs::create_dir_all(parent)?;
        if !self.absolute_path.exists() {
            return Err(anyhow!("corrupted repository"));
        }
        fs::copy(&self.absolute_path, &target_path)?;

        log::debug!("[{}] saved data to {}", getpid(), &target_path.to_string_lossy());
        Ok(())
    }

    pub fn relative_path(&self) -> &str {
        &self.relative_path
    }

    pub fn original_source_path(&self) -> &str {
        &self.original_source_path
    }

    pub fn version(&self) -> &Version {
        &self.version
    }

    pub fn id(&self) -> &ItemId {
        &self.id
    }
}

impl Display for RepositoryItem {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        write!(f, "'{}' : {}", self.original_source_path(), hex::encode(self.id()))
    }
}