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
/*
* Copyright (C) 2021  Aravinth Manivannan <realaravinth@batsense.net>
*
* Use of this source code is governed by the Apache 2.0 and/or the MIT
* License.
*/

use std::collections::HashMap;

#[derive(Debug, Default, Clone)]
pub struct Files {
    pub map: HashMap<String, String>,
    base_dir: String,
}

impl Files {
    pub fn get<'a>(&'a self, path: &'a str) -> Option<&'a String> {
        self.map.get(path)
    }

    pub fn add(&mut self, k: String, v: String) -> Result<(), &'static str> {
        if self.map.contains_key(&k) {
            Err("key exists")
        } else {
            self.map.insert(k, v);
            Ok(())
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::hash::*;

    use super::*;
    use std::path::Path;

    #[test]
    fn get_works() {
        let types = vec![
            mime::IMAGE_PNG,
            mime::IMAGE_SVG,
            mime::IMAGE_JPEG,
            mime::IMAGE_GIF,
        ];

        let config = BusterBuilder::default()
            .source("./dist")
            .result("/tmp/prod2")
            .mime_types(types)
            .copy(true)
            .follow_links(true)
            .build()
            .unwrap();

        config.init().unwrap();
        let files = config.hash().unwrap();

        assert!(file_exists("./dist/log-out.svg", &files));
        assert!(file_exists(
            "./dist/a/b/c/d/s/d/svg/credit-card.svg",
            &files
        ));

        assert!(!file_exists("dist/log-out.svg", &files));
        assert!(!file_exists("dist/a/b/c/d/s/d/svg/credit-card.svg", &files));
    }

    fn file_exists(path: &str, files: &Files) -> bool {
        if let Some(file) = files.get(path) {
            Path::new(file).exists()
        } else {
            false
        }
    }
}