liquid_compiler/
block.rs

1use liquid_error::Result;
2use liquid_interpreter::Renderable;
3
4use super::Language;
5use super::TagBlock;
6use super::TagTokenIter;
7
8pub trait BlockReflection {
9    fn start_tag(&self) -> &'static str;
10
11    fn end_tag(&self) -> &'static str;
12
13    fn description(&self) -> &'static str;
14
15    fn example(&self) -> Option<&'static str> {
16        None
17    }
18
19    fn spec(&self) -> Option<&'static str> {
20        None
21    }
22}
23
24/// A trait for creating custom custom block-size tags (`{% if something %}{% endif %}`).
25/// This is a simple type alias for a function.
26///
27/// This function will be called whenever the parser encounters a block and returns
28/// a new `Renderable` based on its parameters. The received parameters specify the name
29/// of the block, the argument [Tokens](lexer/enum.Token.html) passed to
30/// the block, a Vec of all [Elements](lexer/enum.Element.html) inside the block and
31/// the global [`Language`](struct.Language.html).
32pub trait ParseBlock: Send + Sync + ParseBlockClone + BlockReflection {
33    fn parse(
34        &self,
35        arguments: TagTokenIter,
36        block: TagBlock,
37        options: &Language,
38    ) -> Result<Box<Renderable>>;
39}
40
41pub trait ParseBlockClone {
42    fn clone_box(&self) -> Box<ParseBlock>;
43}
44
45impl<T> ParseBlockClone for T
46where
47    T: 'static + ParseBlock + Clone,
48{
49    fn clone_box(&self) -> Box<ParseBlock> {
50        Box::new(self.clone())
51    }
52}
53
54impl Clone for Box<ParseBlock> {
55    fn clone(&self) -> Box<ParseBlock> {
56        self.clone_box()
57    }
58}
59
60impl<T> From<T> for Box<ParseBlock>
61where
62    T: 'static + ParseBlock,
63{
64    fn from(filter: T) -> Self {
65        Box::new(filter)
66    }
67}