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
//! Converter traits for things that can be converted into tokens.

use super::{Item, Lang, LangBox, Tokens};

/// Helper trait to convert something into a tokens registration.
pub trait RegisterTokens<L>
where
    L: Lang,
{
    /// Convert the type into tokens.
    fn register_tokens(self, tokens: &mut Tokens<L>);
}

impl<T, L> RegisterTokens<L> for T
where
    T: Into<LangBox<L>>,
    L: Lang,
{
    fn register_tokens(self, tokens: &mut Tokens<L>) {
        tokens.item(Item::Registered(self.into()))
    }
}

macro_rules! impl_register_tokens {
    ($($ty:ident => $var:ident),*) => {
        impl<L, $($ty,)*> RegisterTokens<L> for ($($ty,)*)
        where
            $($ty: Into<LangBox<L>>,)*
            L: Lang,
        {
            fn register_tokens(self, tokens: &mut Tokens<L>) {
                let ($($var,)*) = self;
                $(tokens.item(Item::Registered($var.into()));)*
            }
        }
    }
}

impl_register_tokens!(A => a);
impl_register_tokens!(A => a, B => b);
impl_register_tokens!(A => a, B => b, C => c);
impl_register_tokens!(A => a, B => b, C => c, D => d);
impl_register_tokens!(A => a, B => b, C => c, D => d, E => e);
impl_register_tokens!(A => a, B => b, C => c, D => d, E => e, F => f);
impl_register_tokens!(A => a, B => b, C => c, D => d, E => e, F => f, G => g);
impl_register_tokens!(A => a, B => b, C => c, D => d, E => e, F => f, G => g, H => h);