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
//! Macros in GenCo

/// Helper macro to reduce boilerplate needed with nested token expressions.
///
/// ## Examples
///
/// ```rust,ignore
/// let n1: genco::Tokens<()> = toks!("var v = ", "bar".quoted(), ";");
/// ```
#[macro_export]
macro_rules! toks {
    ($($x:expr),*) => {
        {
            let mut _t = $crate::Tokens::new();
            $(_t.append($x);)*
            _t
        }
    };

    ($($x:expr,)*) => {toks!($($x),*)}
}

macro_rules! into_tokens_impl_from {
    ($type:ty, $custom:ty) => {
        impl<'el> From<$type> for Tokens<'el, $custom> {
            fn from(value: $type) -> Tokens<'el, $custom> {
                value.into_tokens()
            }
        }
    }
}

macro_rules! into_tokens_impl_from_generic {
    ($type:ty) => {
        impl<'el, C> From<$type> for Tokens<'el, C> {
            fn from(value: $type) -> Tokens<'el, C> {
                value.into_tokens()
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use quoted::Quoted;
    use tokens::Tokens;
    use js::JavaScript;

    #[test]
    fn test_quoted() {
        let n1: Tokens<JavaScript> = toks!("var v = ", "bar".quoted(), ";");
        assert_eq!("var v = \"bar\";", n1.to_string().unwrap().as_str());
    }
}