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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#![doc = include_str!("../README.md")]
pub use proc_macro2;

use proc_macro2::*;
pub use quote2_macros::quote;
use std::{borrow::Cow, iter, rc::Rc};

impl TokensExt for TokenStream {
    fn add<U>(&mut self, token: U)
    where
        U: Into<TokenTree>,
    {
        self.extend(iter::once(token.into()))
    }

    fn tokens(&mut self, tts: impl IntoTokens) {
        tts.into_tokens(self)
    }
}

pub trait TokensExt {
    fn add<U>(&mut self, token: U)
    where
        U: Into<TokenTree>;

    fn punct_join(&mut self, ch: char) {
        self.add(Punct::new(ch, Spacing::Joint));
    }
    fn punct(&mut self, ch: char) {
        self.add(Punct::new(ch, Spacing::Alone));
    }

    fn ident(&mut self, name: &str) {
        self.add(Ident::new(name, Span::call_site()));
    }

    fn ident_span(&mut self, name: &str, span: Span) {
        self.add(Ident::new(name, span));
    }

    fn group(&mut self, delimiter: char, f: impl FnOnce(&mut TokenStream)) {
        let delimiter = match delimiter {
            '{' => Delimiter::Brace,
            '[' => Delimiter::Bracket,
            '(' => Delimiter::Parenthesis,
            _ => Delimiter::None,
        };
        self.add(group(delimiter, f));
    }

    fn tokens(&mut self, _: impl IntoTokens);
}

pub fn group(delimiter: Delimiter, f: impl FnOnce(&mut TokenStream)) -> Group {
    let mut stream = TokenStream::new();
    f(&mut stream);
    Group::new(delimiter, stream)
}

pub trait IntoTokens {
    fn into_tokens(self, s: &mut TokenStream);
}

impl<'a, T> IntoTokens for &'a T
where
    T: ?Sized + IntoTokens + Clone,
{
    fn into_tokens(self, s: &mut TokenStream) {
        T::into_tokens(T::clone(self), s)
    }
}

impl<T: IntoTokens> IntoTokens for Option<T> {
    fn into_tokens(self, s: &mut TokenStream) {
        match self {
            Some(v) => T::into_tokens(v, s),
            None => {}
        }
    }
}

macro_rules! impl_for {
    [@lit $($lit: ty => $name: ident)*] => {$(
        impl IntoTokens for $lit {
            fn into_tokens(self, s: &mut TokenStream) {
                s.add(Literal::$name(self));
            }
        }
    )*};
    [@struct $($ty: ty)*] => {$(
        impl IntoTokens for $ty {
            fn into_tokens(self, s: &mut TokenStream) {
                s.add(self)
            }
        }
    )*};
}

impl_for! {@struct Group Ident Punct Literal TokenTree }
impl_for! {
    @lit
    u8 => u8_suffixed
    u16 => u16_suffixed
    u32 => u32_suffixed
    u64 => u64_suffixed
    u128 => u128_suffixed

    i8 => i8_suffixed
    i16 => i16_suffixed
    i32 => i32_suffixed
    i64 => i64_suffixed
    i128 => i128_suffixed

    f32 => f32_suffixed
    f64 => f64_suffixed

    &str => string
    char => character
    &[u8] => byte_string
}

impl IntoTokens for bool {
    fn into_tokens(self, s: &mut TokenStream) {
        let word = if self { "true" } else { "false" };
        s.add(Ident::new(word, Span::call_site()));
    }
}

impl IntoTokens for TokenStream {
    fn into_tokens(self, s: &mut TokenStream) {
        s.extend(iter::once(self));
    }
}

impl<'a, T: ?Sized + IntoTokens + Clone> IntoTokens for &'a mut T {
    fn into_tokens(self, s: &mut TokenStream) {
        T::into_tokens(self.clone(), s)
    }
}

impl<'a, T: ?Sized + IntoTokens + Clone> IntoTokens for Cow<'a, T> {
    fn into_tokens(self, s: &mut TokenStream) {
        match self {
            Cow::Borrowed(v) => T::into_tokens(T::clone(v), s),
            Cow::Owned(v) => T::into_tokens(v, s),
        }
    }
}

impl<T: IntoTokens> IntoTokens for Box<T> {
    fn into_tokens(self, s: &mut TokenStream) {
        T::into_tokens(*self, s)
    }
}

impl<T: IntoTokens + Clone> IntoTokens for Rc<T> {
    fn into_tokens(self, s: &mut TokenStream) {
        T::into_tokens(T::clone(&self), s)
    }
}

#[derive(Debug, Clone, Copy)]
pub struct Quote<F>(F)
where
    F: for<'a> FnOnce(&'a mut TokenStream);

impl<F> IntoTokens for Quote<F>
where
    F: for<'a> FnOnce(&'a mut TokenStream),
{
    fn into_tokens(self, s: &mut TokenStream) {
        self.0(s)
    }
}

pub fn quote<F>(f: F) -> Quote<F>
where
    F: for<'a> FnOnce(&'a mut TokenStream),
{
    Quote(f)
}