loose_liquid_core/partials/
mod.rs

1use std::borrow;
2use std::fmt;
3use std::sync;
4
5use crate::error::Error;
6use crate::error::Result;
7use crate::parser::Language;
8use crate::runtime::PartialStore;
9
10mod eager;
11mod inmemory;
12mod lazy;
13mod ondemand;
14
15pub use self::eager::*;
16pub use self::inmemory::*;
17pub use self::lazy::*;
18pub use self::ondemand::*;
19
20/// Compile a `PartialSource` into a `PartialStore` of `Renderable`s.
21///
22/// This trait is intended to allow a variety of implementation/policies to fit your needs,
23/// including:
24/// - Compile partials eagerly or lazily.
25/// - Report compile errors eagerly or lazily.
26/// - Whether to cache the results or not.
27pub trait PartialCompiler {
28    /// Convert a `PartialSource` into a `PartialStore`.
29    fn compile(self, language: sync::Arc<Language>) -> Result<Box<dyn PartialStore + Send + Sync>>;
30
31    /// Access underlying `PartialSource`
32    fn source(&self) -> &dyn PartialSource;
33}
34
35/// Partial-template source repository.
36pub trait PartialSource: fmt::Debug {
37    /// Check if partial-template exists.
38    fn contains(&self, name: &str) -> bool;
39
40    /// Enumerate all partial-templates.
41    fn names(&self) -> Vec<&str>;
42
43    /// Access a partial-template.
44    fn try_get<'a>(&'a self, name: &str) -> Option<borrow::Cow<'a, str>>;
45
46    /// Access a partial-template
47    fn get<'a>(&'a self, name: &str) -> Result<borrow::Cow<'a, str>> {
48        self.try_get(name).ok_or_else(|| {
49            let mut available: Vec<_> = self.names();
50            available.sort_unstable();
51            let available = itertools::join(available, ", ");
52            Error::with_msg("Unknown partial-template")
53                .context("requested partial", name.to_owned())
54                .context("available partials", available)
55        })
56    }
57}