liquid_core/parser/
tag.rs

1use crate::error::Result;
2use crate::runtime::Renderable;
3
4use super::Language;
5use super::TagTokenIter;
6
7pub trait TagReflection {
8    fn tag(&self) -> &str;
9
10    fn description(&self) -> &str;
11
12    fn example(&self) -> Option<&str> {
13        None
14    }
15
16    fn spec(&self) -> Option<&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] based on its parameters. The received parameters
25/// specify the name of the tag, the argument [Tokens](crate::TagTokenIter) passed to
26/// the tag and the global [`Language`].
27pub trait ParseTag: Send + Sync + ParseTagClone {
28    fn parse(&self, arguments: TagTokenIter, options: &Language) -> Result<Box<dyn Renderable>>;
29
30    fn reflection(&self) -> &dyn TagReflection;
31}
32
33pub trait ParseTagClone {
34    fn clone_box(&self) -> Box<dyn ParseTag>;
35}
36
37impl<T> ParseTagClone for T
38where
39    T: 'static + ParseTag + Clone,
40{
41    fn clone_box(&self) -> Box<dyn ParseTag> {
42        Box::new(self.clone())
43    }
44}
45
46impl Clone for Box<dyn ParseTag> {
47    fn clone(&self) -> Box<dyn ParseTag> {
48        self.clone_box()
49    }
50}
51
52impl<T> From<T> for Box<dyn ParseTag>
53where
54    T: 'static + ParseTag,
55{
56    fn from(filter: T) -> Self {
57        Box::new(filter)
58    }
59}