summaryrefslogtreecommitdiffstats
path: root/src/filestore/artifact.rs
blob: 3b9f2e05619ad88b3b807dc6de3af5bbcf432d39 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use std::cmp::Ordering;

use anyhow::Context;
use anyhow::Error;
use anyhow::Result;
use anyhow::anyhow;
use getset::Getters;
use pom::parser::Parser as PomParser;

use crate::package::PackageName;
use crate::package::PackageVersion;
use crate::filestore::path::*;

#[derive(Clone, PartialEq, Eq, Debug, Getters)]
pub struct Artifact {
    #[getset(get = "pub")]
    path: ArtifactPath,

    #[getset(get = "pub")]
    name: PackageName,

    #[getset(get = "pub")]
    version: PackageVersion,
}

impl PartialOrd for Artifact {
    fn partial_cmp(&self, other: &Artifact) -> Option<Ordering> {
        self.version.partial_cmp(&other.version)
    }
}

impl Ord for Artifact {
    fn cmp(&self, other: &Self) -> Ordering {
        self.version.cmp(&other.version)
    }
}


impl Artifact {
    pub fn load(root: &StoreRoot, path: ArtifactPath) -> Result<Self> {
        let joined_fullpath = root.join(&path);
        if joined_fullpath.is_file() {
            let (name, version) = Self::parse_path(root, &path)
                .with_context(|| anyhow!("Pathing artifact path: '{}'", joined_fullpath.display()))?;

            Ok(Artifact {
                path,
                name,
                version
            })
        } else {
            if path.is_dir() {
                Err(anyhow!("Cannot load non-file path: {}", path.display()))
            } else {
                Err(anyhow!("Path does not exist: {}", path.display()))
            }
        }
    }

    fn parse_path(root: &StoreRoot, path: &ArtifactPath) -> Result<(PackageName, PackageVersion)> {
        path.file_stem()
            .ok_or_else(|| anyhow!("Cannot get filename from {}", (root.join(path)).display()))?
            .to_owned()
            .into_string()
            .map_err(|_| anyhow!("Internal conversion of '{}' to UTF-8", (root.join(path)).display()))
            .and_then(|s| Self::parser().parse(s.as_bytes()).map_err(Error::from))
    }

    /// Construct a parser that parses a Vec<u8> into (PackageName, PackageVersion)
    fn parser<'a>() -> PomParser<'a, u8, (PackageName, PackageVersion)> {
        (PackageName::parser() + crate::util::parser::dash() + PackageVersion::parser())
            .map(|((name, _), vers)| (name, vers))
    }

}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::package::tests::pname;
    use crate::package::tests::pversion;
    use crate::filestore::path::StoreRoot;
    use crate::filestore::path::ArtifactPath;
    use std::path::PathBuf;

    #[test]
    fn test_parser_one_letter_name() {
        let p = ArtifactPath::new(PathBuf::from("a-1.ext")).unwrap();
        let root = StoreRoot::new(PathBuf::from("/"));
        let r = Artifact::parse_path(&root, &p);

        assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r);
        let (name, version) = r.unwrap();

        assert_eq!(name, pname("a"));
        assert_eq!(version, pversion("1"));
    }

    #[test]
    fn test_parser_multi_letter_name() {
        let p = ArtifactPath::new(PathBuf::from("foo-1.ext")).unwrap();
        let root = StoreRoot::new(PathBuf::from("/"));
        let r = Artifact::parse_path(&root, &p);

        assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r);
        let (name, version) = r.unwrap();

        assert_eq!(name, pname("foo"));
        assert_eq!(version, pversion("1"));
    }

    #[test]
    fn test_parser_multi_char_version() {
        let p = ArtifactPath::new(PathBuf::from("foo-1123.ext")).unwrap();
        let root = StoreRoot::new(PathBuf::from("/"));
        let r = Artifact::parse_path(&root, &p);

        assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r);
        let (name, version) = r.unwrap();

        assert_eq!(name, pname("foo"));
        assert_eq!(version, pversion("1123"));
    }

    #[test]
    fn test_parser_multi_char_version_dashed() {
        let p = ArtifactPath::new(PathBuf::from("foo-1-1-2-3.ext")).unwrap();
        let root = StoreRoot::new(PathBuf::from("/"));
        let r = Artifact::parse_path(&root, &p);

        assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r);
        let (name, version) = r.unwrap();

        assert_eq!(name, pname("foo"));
        assert_eq!(version, pversion("1-1-2-3"));
    }

    #[test]
    fn test_parser_multi_char_version_dashed_and_dotted() {
        let p = ArtifactPath::new(PathBuf::from("foo-1-1.2-3.ext")).unwrap();
        let root = StoreRoot::new(PathBuf::from("/"));
        let r = Artifact::parse_path(&root, &p);

        assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r);
        let (name, version) = r.unwrap();

        assert_eq!(name, pname("foo"));
        assert_eq!(version, pversion("1-1.2-3"));
    }

    #[test]
    fn test_parser_alnum_version() {
        let p = ArtifactPath::new(PathBuf::from("foo-1-1.2a3.ext")).unwrap();
        let root = StoreRoot::new(PathBuf::from("/"));
        let r = Artifact::parse_path(&root, &p);

        assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r);
        let (name, version) = r.unwrap();

        assert_eq!(name, pname("foo"));
        assert_eq!(version, pversion("1-1.2a3"));
    }

    #[test]
    fn test_parser_package_name_with_number() {
        let p = ArtifactPath::new(PathBuf::from("foo2-1-1.2a3.ext")).unwrap();
        let root = StoreRoot::new(PathBuf::from("/"));
        let r = Artifact::parse_path(&root, &p);

        assert!(r.is_ok(), "Expected to be Ok(_): {:?}", r);
        let (name, version) = r.unwrap();

        assert_eq!(name, pname("foo2"));
        assert_eq!(version, pversion("1-1.2a3"));
    }
}