liquid_core/partials/
inmemory.rs

1use std::borrow;
2use std::collections::HashMap;
3
4use super::PartialSource;
5
6/// In-memory collection of partial-template source code.
7#[derive(Debug, Default, Clone)]
8pub struct InMemorySource {
9    data: HashMap<String, String>,
10}
11
12impl InMemorySource {
13    /// Create an in-memory repository to store partial-template source.
14    pub fn new() -> Self {
15        Default::default()
16    }
17
18    /// Add a partial-template's source.
19    pub fn add<N, S>(&mut self, name: N, source: S) -> bool
20    where
21        N: Into<String>,
22        S: Into<String>,
23    {
24        self.data.insert(name.into(), source.into()).is_some()
25    }
26}
27
28impl PartialSource for InMemorySource {
29    fn contains(&self, name: &str) -> bool {
30        self.data.contains_key(name)
31    }
32
33    fn names(&self) -> Vec<&str> {
34        self.data.keys().map(|s| s.as_str()).collect()
35    }
36
37    fn try_get<'a>(&'a self, name: &str) -> Option<borrow::Cow<'a, str>> {
38        self.data.get(name).map(|s| s.as_str().into())
39    }
40}