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
use std::{
    fs::File,
    path::{Path, PathBuf},
    time::UNIX_EPOCH,
};

use crate::{source::Source, ErrorKind, Result, ResultExt};

/// Directory source.
///
/// Please note that there is a default directory source
/// inside the `Loader`, which is automatically used when you call
/// `load`. In case you want another, second, directory for assets,
/// you can instantiate one yourself, too. Please use `Loader::load_from` then.
#[derive(Debug)]
pub struct Directory {
    loc: PathBuf,
}

impl Directory {
    /// Creates a new directory storage.
    pub fn new<P>(loc: P) -> Self
    where
        P: Into<PathBuf>,
    {
        Directory { loc: loc.into() }
    }

    fn path(&self, s_path: &str) -> PathBuf {
        let mut path = self.loc.clone();
        path.extend(Path::new(s_path).iter());

        path
    }
}

impl Source for Directory {
    fn modified(&self, path: &str) -> Result<u64> {
        #[cfg(feature = "profiler")]
        profile_scope!("dir_modified_asset");
        use std::fs::metadata;

        let path = self.path(path);

        metadata(&path)
            .chain_err(|| format!("Failed to fetch metadata for {:?}", path))?
            .modified()
            .chain_err(|| "Could not get modification time")?
            .duration_since(UNIX_EPOCH)
            .chain_err(|| "Anomalies with the system clock caused `duration_since` to fail")
            .map(|d| d.as_secs())
    }

    fn load(&self, path: &str) -> Result<Vec<u8>> {
        #[cfg(feature = "profiler")]
        profile_scope!("dir_load_asset");
        use std::io::Read;

        let path = self.path(path);

        let mut v = Vec::new();
        let mut file = File::open(&path)
            .chain_err(|| format!("Failed to open file {:?}", path))
            .chain_err(|| ErrorKind::Source)?;
        file.read_to_end(&mut v)
            .chain_err(|| format!("Failed to read file {:?}", path))
            .chain_err(|| ErrorKind::Source)?;

        Ok(v)
    }
}

#[cfg(test)]
mod test {
    use std::path::Path;

    use crate::source::Source;

    use super::Directory;

    #[test]
    fn loads_asset_from_assets_directory() {
        let test_assets_dir = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/assets");
        let directory = Directory::new(test_assets_dir);

        assert_eq!(
            "data".as_bytes().to_vec(),
            directory
                .load("subdir/asset")
                .expect("Failed to load tests/assets/subdir/asset")
        );
    }

    #[cfg(windows)]
    #[test]
    fn tolerates_backslashed_location_with_forward_slashed_asset_paths() {
        // Canonicalized path on Windows uses backslashes
        let test_assets_dir = Path::new(env!("CARGO_MANIFEST_DIR"))
            .join("tests/assets")
            .canonicalize()
            .expect("Failed to canonicalize tests/assets directory");
        let directory = Directory::new(test_assets_dir);

        assert_eq!(
            "data".as_bytes().to_vec(),
            // Use forward slash to declare path
            directory
                .load("subdir/asset")
                .expect("Failed to load tests/assets/subdir/asset")
        );
    }
}