brrrty_js/loader.rs
1//! Finding included templates by name: the loader Brrrty's template
2//! engines share.
3
4/// Why a [`Loader`] could not supply a template.
5///
6/// `io` is `None` when no root has the file: the template is missing, and
7/// `tried` lists every path looked at. `io` is `Some` when a file was found
8/// but could not be used: it can't be read, isn't UTF-8, or resolves
9/// outside the loader's roots.
10#[derive(Debug, Default)]
11pub struct LoadError {
12 pub tried: Vec<String>,
13 pub io: Option<std::io::Error>,
14}
15
16/// Finds included templates by name. Returns the resolved path and the
17/// source.
18///
19/// The engine does its own name handling first (LiquidJS appends
20/// `.liquid` to a name with no extension; Nunjucks resolves `./` and `../`
21/// names against the including template's path) and calls `load` with the
22/// result. The loader only maps a name to a file: it tries each of its
23/// roots in order (11ty's are the includes folder, then the input folder
24/// for Liquid or the project folder for Nunjucks), refuses names that
25/// leave every root, and returns the path it found and the source.
26/// Templates are told apart by that path, so it should be canonical: a
27/// cycle is then found however the name is spelled.
28pub trait Loader: Send + Sync {
29 fn load(&self, name: &str) -> Result<(String, String), LoadError>;
30}