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
use super::{LoadError, Loader, SourceFile, SourceName};
use std::path::{Path, PathBuf};

/// A [`Loader`] that loads files from the filesystem.
#[derive(Debug)]
pub struct FsLoader {
    path: Vec<PathBuf>,
}

impl FsLoader {
    /// Create a new `FsFileContext`.
    ///
    /// Files will be resolved from the current working directory.
    pub fn for_cwd() -> Self {
        Self {
            path: vec![PathBuf::new()],
        }
    }

    /// Add a path to search for files.
    pub fn push_path(&mut self, path: &Path) {
        self.path.push(path.into());
    }

    /// Create a loader and a `SourceFile` from a given `Path`.
    pub fn for_path(path: &Path) -> Result<(Self, SourceFile), LoadError> {
        let mut f = std::fs::File::open(path)
            .map_err(|e| LoadError::Input(path.display().to_string(), e))?;

        let (path, name) = path
            .parent()
            .and_then(|base| {
                Some((
                    vec![base.to_path_buf()],
                    path.strip_prefix(base).ok()?,
                ))
            })
            .unwrap_or_else(|| (vec![PathBuf::new()], path));
        let loader = Self { path };
        let source = SourceName::root(name.display().to_string());
        let source = SourceFile::read(&mut f, source)?;
        Ok((loader, source))
    }
}

impl Loader for FsLoader {
    type File = std::fs::File;

    fn find_file(&self, url: &str) -> Result<Option<Self::File>, LoadError> {
        if !url.is_empty() {
            for base in &self.path {
                let full = base.join(url);
                if full.is_file() {
                    tracing::debug!(?full, "opening file");
                    return Self::File::open(&full)
                        .map_err(|e| {
                            LoadError::Input(full.display().to_string(), e)
                        })
                        .map(Some);
                }
                tracing::trace!(?full, "Not found");
            }
        }
        Ok(None)
    }
}