summaryrefslogtreecommitdiffstats
path: root/src/filestore/path.rs
blob: 31e4bb03f7fb53baa577132934d44d7d9fee5a4b (plain)
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use std::path::Path;
use std::path::PathBuf;
use std::ffi::OsStr;

use anyhow::Error;
use anyhow::Result;

#[derive(Debug)]
pub struct StoreRoot(PathBuf);

impl StoreRoot {
    pub (in crate::filestore) fn new(root: PathBuf) -> Self {
        StoreRoot(root)
    }

    pub (in crate::filestore) fn stripped_from(&self, pb: &Path) -> Result<ArtifactPath> {
        pb.strip_prefix(&self.0)
            .map(|p| ArtifactPath::new(p.to_path_buf()))
            .map_err(Error::from)
    }

    pub fn join(&self, ap: &ArtifactPath) -> FullArtifactPath {
        let join = self.0.join(&ap.0);
        FullArtifactPath(join)
    }

    // Needed for FileStoreImpl::path_exists_in_store_root()
    pub (in crate::filestore) fn join_path(&self, p: &Path) -> PathBuf {
        self.0.join(p)
    }

    pub fn display(&self) -> std::path::Display {
        self.0.display()
    }
}

impl AsRef<Path> for StoreRoot {
    fn as_ref(&self) -> &Path {
        &self.0
    }
}


#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ArtifactPath(PathBuf);

impl ArtifactPath {
    pub (in crate::filestore) fn new(p: PathBuf) -> Self {
        ArtifactPath(p)
    }

    pub fn display(&self) -> std::path::Display {
        self.0.display()
    }

    pub fn file_name(&self) -> Option<&OsStr> {
        self.0.file_name()
    }

    pub fn to_str(&self) -> Option<&str> {
        self.0.to_str()
    }

    pub (in crate::filestore) fn file_stem(&self) -> Option<&OsStr> {
        self.0.file_stem()
    }

    pub (in crate::filestore) fn is_dir(&self) -> bool {
        self.0.is_dir()
    }
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FullArtifactPath(PathBuf);

impl AsRef<Path> for FullArtifactPath {
    fn as_ref(&self) -> &Path {
        self.0.as_ref()
    }
}

impl FullArtifactPath {
    pub (in crate::filestore) fn is_dir(&self) -> bool {
        self.0.is_dir()
    }

    pub (in crate::filestore) fn is_file(&self) -> bool {
        self.0.is_file()
    }

    pub fn display(&self) -> std::path::Display {
        self.0.display()
    }
}