loose_liquid_core/parser/
block.rs

1use crate::error::Result;
2use crate::runtime::Renderable;
3
4use super::Language;
5use super::TagBlock;
6use super::TagTokenIter;
7
8pub trait BlockReflection {
9    fn start_tag(&self) -> &str;
10
11    fn end_tag(&self) -> &str;
12
13    fn description(&self) -> &str;
14
15    fn example(&self) -> Option<&str> {
16        None
17    }
18
19    fn spec(&self) -> Option<&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](crate::TagTokenIter) passed to
30/// the block, a [TagBlock](crate::TagBlock) inside the block and
31/// the global [`Language`].
32pub trait ParseBlock: Send + Sync + ParseBlockClone {
33    fn parse(
34        &self,
35        arguments: TagTokenIter,
36        block: TagBlock,
37        options: &Language,
38    ) -> Result<Box<dyn Renderable>>;
39
40    fn reflection(&self) -> &dyn BlockReflection;
41}
42
43pub trait ParseBlockClone {
44    fn clone_box(&self) -> Box<dyn ParseBlock>;
45}
46
47impl<T> ParseBlockClone for T
48where
49    T: 'static + ParseBlock + Clone,
50{
51    fn clone_box(&self) -> Box<dyn ParseBlock> {
52        Box::new(self.clone())
53    }
54}
55
56impl Clone for Box<dyn ParseBlock> {
57    fn clone(&self) -> Box<dyn ParseBlock> {
58        self.clone_box()
59    }
60}
61
62impl<T> From<T> for Box<dyn ParseBlock>
63where
64    T: 'static + ParseBlock,
65{
66    fn from(filter: T) -> Self {
67        Box::new(filter)
68    }
69}