1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use crate::error::Result;
use crate::runtime::Renderable;

use super::Language;
use super::TagBlock;
use super::TagTokenIter;

pub trait BlockReflection {
    fn start_tag(&self) -> &str;

    fn end_tag(&self) -> &str;

    fn description(&self) -> &str;

    fn example(&self) -> Option<&str> {
        None
    }

    fn spec(&self) -> Option<&str> {
        None
    }
}

/// A trait for creating custom custom block-size tags (`{% if something %}{% endif %}`).
/// This is a simple type alias for a function.
///
/// This function will be called whenever the parser encounters a block and returns
/// a new `Renderable` based on its parameters. The received parameters specify the name
/// of the block, the argument [Tokens](lexer/enum.Token.html) passed to
/// the block, a Vec of all [Elements](lexer/enum.Element.html) inside the block and
/// the global [`Language`](struct.Language.html).
pub trait ParseBlock: Send + Sync + ParseBlockClone {
    fn parse(
        &self,
        arguments: TagTokenIter,
        block: TagBlock,
        options: &Language,
    ) -> Result<Box<dyn Renderable>>;

    fn reflection(&self) -> &dyn BlockReflection;
}

pub trait ParseBlockClone {
    fn clone_box(&self) -> Box<dyn ParseBlock>;
}

impl<T> ParseBlockClone for T
where
    T: 'static + ParseBlock + Clone,
{
    fn clone_box(&self) -> Box<dyn ParseBlock> {
        Box::new(self.clone())
    }
}

impl Clone for Box<dyn ParseBlock> {
    fn clone(&self) -> Box<dyn ParseBlock> {
        self.clone_box()
    }
}

impl<T> From<T> for Box<dyn ParseBlock>
where
    T: 'static + ParseBlock,
{
    fn from(filter: T) -> Self {
        Box::new(filter)
    }
}