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
use std::{
    cell::Cell,
    collections::hash_map::{Entry, HashMap},
    fs, io,
    path::{Path, PathBuf},
};

use super::Source;

/// Source for reading files from filesystem.
pub struct Fs {
    inc_dirs: Vec<PathBuf>,
    cache: Cell<Option<HashMap<PathBuf, String>>>,
}

impl Default for Fs {
    fn default() -> Self {
        Fs {
            inc_dirs: Vec::new(),
            cache: Cell::new(Some(HashMap::new())),
        }
    }
}

impl Fs {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn builder() -> FsBuilder {
        FsBuilder {
            source: Self::new(),
        }
    }

    pub fn include_dir(&mut self, dir: &Path) -> io::Result<()> {
        self.check_dir(dir)?;
        self.inc_dirs.push(dir.to_path_buf());
        Ok(())
    }

    fn check_dir(&self, dir: &Path) -> io::Result<()> {
        let meta = fs::metadata(dir)?;
        if !meta.is_dir() {
            Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!("'{}' is not a directory", dir.display()),
            ))
        } else {
            Ok(())
        }
    }

    fn check_file(&self, path: &Path) -> io::Result<()> {
        let map = self.cache.take().unwrap();
        let contains = map.contains_key(path);
        self.cache.set(Some(map));
        if contains {
            return Ok(());
        }

        match fs::metadata(path) {
            Ok(meta) => {
                if !meta.is_file() {
                    Err(io::Error::new(
                        io::ErrorKind::InvalidData,
                        format!("'{}' is not a file", path.display()),
                    ))
                } else {
                    Ok(())
                }
            }
            Err(e) => Err(e),
        }
    }

    fn find_in_dir(&self, dir: &Path, name: &Path) -> io::Result<Option<PathBuf>> {
        let path = dir.join(name);
        match self.check_file(&path) {
            Ok(()) => Ok(Some(path)),
            Err(e) => match e.kind() {
                io::ErrorKind::NotFound => Ok(None),
                _ => Err(e),
            },
        }
    }

    fn find_file(&self, dir: Option<&Path>, name: &Path) -> io::Result<PathBuf> {
        if name.is_absolute() {
            return Ok(name.to_path_buf());
        }

        if let Some(dir) = dir {
            if let Some(path) = self.find_in_dir(dir, name)? {
                return Ok(path);
            }
        }

        for dir in self.inc_dirs.iter() {
            if let Some(path) = self.find_in_dir(dir, name)? {
                return Ok(path);
            }
        }

        Err(io::Error::new(
            io::ErrorKind::NotFound,
            name.to_string_lossy(),
        ))
    }
}

pub struct FsBuilder {
    source: Fs,
}

impl FsBuilder {
    pub fn include_dir(mut self, dir: &Path) -> io::Result<Self> {
        self.source.include_dir(dir).map(|()| self)
    }

    pub fn build(self) -> Fs {
        self.source
    }
}

impl Source for Fs {
    fn read(&self, path: &Path, dir: Option<&Path>) -> io::Result<(PathBuf, String)> {
        self.find_file(dir, path).and_then(|path| {
            let mut map = self.cache.take().unwrap();

            let res = match map.entry(path.clone()) {
                Entry::Occupied(v) => Ok(v.get().clone()),
                Entry::Vacant(v) => fs::read_to_string(&path).map(|data| {
                    v.insert(data.clone());
                    data
                }),
            }
            .map(|data| (path, data));

            self.cache.set(Some(map));
            res
        })
    }
}