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
use quote::{format_ident, quote_spanned, ToTokens};
use proc_macro2::{Span};
use syn::parse::{ParseStream, Result};
use syn::{Expr, bracketed};
use syn::token::{Bracket};
pub struct BracketedExpr {
pub bracket: Bracket,
pub context: String,
pub expr: Expr
}
impl ToTokens for BracketedExpr {
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
let ref expr = self.expr;
let coerce = format_ident!("to_{}", self.context, span=self.bracket.span);
(quote_spanned! {self.bracket.span=>
stream.push_str(&#expr.#coerce());
}).to_tokens(tokens);
}
}
impl BracketedExpr {
pub fn span(&self) -> Span {
self.bracket.span
}
pub fn parse(context: String, input: ParseStream) -> Result<Self> {
let content;
let content2;
let bracket1 = bracketed!(content in input);
let _bracket2 = bracketed!(content2 in content);
let expr: Expr = content2.parse()?;
Ok(BracketedExpr{ bracket:bracket1, context:context, expr:expr })
}
}