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
use std::borrow;
use std::fmt;
use std::sync;

use liquid_compiler::Language;
use liquid_error::Error;
use liquid_error::Result;
use liquid_interpreter::PartialStore;

mod eager;
mod inmemory;
mod lazy;
mod ondemand;

pub use self::eager::*;
pub use self::inmemory::*;
pub use self::lazy::*;
pub use self::ondemand::*;

/// Compile a `PartialSource` into a `PartialStore` of `Renderable`s.
///
/// This trait is intended to allow a variety of implementation/policies to fit your needs,
/// including:
/// - Compile partials eagerly or lazily.
/// - Report compile errors eagerly or lazily.
/// - Whether to cache the results or not.
pub trait PartialCompiler {
    /// Convert a `PartialSource` into a `PartialStore`.
    fn compile(self, language: sync::Arc<Language>) -> Result<Box<PartialStore + Send + Sync>>;
}

/// Partial-template source repository.
pub trait PartialSource: fmt::Debug {
    /// Check if partial-template exists.
    fn contains(&self, name: &str) -> bool;

    /// Enumerate all partial-templates.
    fn names(&self) -> Vec<&str>;

    /// Access a partial-template.
    fn try_get<'a>(&'a self, name: &str) -> Option<borrow::Cow<'a, str>>;

    /// Access a partial-template
    fn get<'a>(&'a self, name: &str) -> Result<borrow::Cow<'a, str>> {
        self.try_get(name).ok_or_else(|| {
            let mut available: Vec<_> = self.names();
            available.sort_unstable();
            let available = itertools::join(available, ", ");
            Error::with_msg("Unknown partial-template")
                .context("requested partial", name.to_owned())
                .context("available partials", available)
        })
    }
}