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
109
110
111
112
113
114
115
116
117
118
119
//! Converter traits for things that can be converted into tokens.

use super::{Cons, Element, ErasedElement, Lang, Tokens};

/// Helper trait to convert something into tokens.
pub trait FormatTokens<'el, L>
where
    L: Lang,
{
    /// Convert the type into tokens.
    fn format_tokens(self, tokens: &mut Tokens<'el, L>);

    /// Hint to test if we are empty.
    fn is_empty(&self) -> bool {
        false
    }
}

impl<'el, L> FormatTokens<'el, L> for Tokens<'el, L>
where
    L: Lang,
{
    fn format_tokens(self, tokens: &mut Self) {
        tokens.elements.extend(self.elements);
    }

    fn is_empty(&self) -> bool {
        self.elements.is_empty()
    }
}

/// Convert collection to tokens.
impl<'el, L> FormatTokens<'el, L> for Vec<Tokens<'el, L>>
where
    L: Lang,
{
    fn format_tokens(self, tokens: &mut Tokens<'el, L>) {
        for t in self {
            tokens.elements.extend(t.elements);
        }
    }

    fn is_empty(&self) -> bool {
        self.iter().all(|t| t.is_empty())
    }
}

/// Convert element to tokens.
impl<'el, L> FormatTokens<'el, L> for Element<'el, L>
where
    L: Lang,
{
    fn format_tokens(self, tokens: &mut Tokens<'el, L>) {
        tokens.elements.push(self);
    }
}

/// Convert an erased element to tokens.
impl<'el, L> FormatTokens<'el, L> for ErasedElement<'el>
where
    L: Lang,
{
    fn format_tokens(self, tokens: &mut Tokens<'el, L>) {
        tokens.elements.push(self.into());
    }
}

/// Convert borrowed strings.
impl<'el, L> FormatTokens<'el, L> for &'el str
where
    L: Lang,
{
    fn format_tokens(self, tokens: &mut Tokens<'el, L>) {
        tokens.elements.push(self.into());
    }
}

/// Convert borrowed strings.
impl<'el, L> FormatTokens<'el, L> for &'el String
where
    L: Lang,
{
    fn format_tokens(self, tokens: &mut Tokens<'el, L>) {
        tokens.elements.push(self.as_str().into());
    }
}

/// Convert strings.
impl<'el, L> FormatTokens<'el, L> for String
where
    L: Lang,
{
    fn format_tokens(self, tokens: &mut Tokens<'el, L>) {
        tokens.elements.push(self.into());
    }
}

/// Convert stringy things.
impl<'el, L> FormatTokens<'el, L> for Cons<'el>
where
    L: Lang,
{
    fn format_tokens(self, tokens: &mut Tokens<'el, L>) {
        tokens.elements.push(self.into());
    }
}

/// Convert stringy things.
impl<'el, L, T> FormatTokens<'el, L> for Option<T>
where
    L: Lang,
    T: FormatTokens<'el, L>,
{
    fn format_tokens(self, tokens: &mut Tokens<'el, L>) {
        if let Some(inner) = self {
            inner.format_tokens(tokens);
        }
    }
}