liquid_compiler/
tag.rs

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