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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
use liquid_interpreter::Renderable;

use super::error::Result;
use super::Element;
use super::LiquidOptions;
use super::Token;

/// 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 [`LiquidOptions`](struct.LiquidOptions.html).
pub trait ParseBlock: Send + Sync + ParseBlockClone {
    fn parse(
        &self,
        tag_name: &str,
        arguments: &[Token],
        tokens: &[Element],
        options: &LiquidOptions,
    ) -> Result<Box<Renderable>>;
}

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

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

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

pub type FnParseBlock = fn(&str, &[Token], &[Element], &LiquidOptions) -> Result<Box<Renderable>>;

#[derive(Clone)]
struct FnBlockParser {
    parser: FnParseBlock,
}

impl FnBlockParser {
    fn new(parser: FnParseBlock) -> Self {
        Self { parser }
    }
}

impl ParseBlock for FnBlockParser {
    fn parse(
        &self,
        tag_name: &str,
        arguments: &[Token],
        tokens: &[Element],
        options: &LiquidOptions,
    ) -> Result<Box<Renderable>> {
        (self.parser)(tag_name, arguments, tokens, options)
    }
}

#[derive(Clone)]
enum BlockParserEnum {
    Fun(FnBlockParser),
    Heap(Box<ParseBlock>),
}

#[derive(Clone)]
pub struct BoxedBlockParser {
    parser: BlockParserEnum,
}

impl ParseBlock for BoxedBlockParser {
    fn parse(
        &self,
        tag_name: &str,
        arguments: &[Token],
        tokens: &[Element],
        options: &LiquidOptions,
    ) -> Result<Box<Renderable>> {
        match self.parser {
            BlockParserEnum::Fun(ref f) => f.parse(tag_name, arguments, tokens, options),
            BlockParserEnum::Heap(ref f) => f.parse(tag_name, arguments, tokens, options),
        }
    }
}

impl From<FnParseBlock> for BoxedBlockParser {
    fn from(parser: FnParseBlock) -> BoxedBlockParser {
        let parser = BlockParserEnum::Fun(FnBlockParser::new(parser));
        Self { parser }
    }
}

impl From<Box<ParseBlock>> for BoxedBlockParser {
    fn from(parser: Box<ParseBlock>) -> BoxedBlockParser {
        let parser = BlockParserEnum::Heap(parser);
        Self { parser }
    }
}