genco_macros/
quote_fn.rs

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
use proc_macro2::TokenStream;
use syn::parse::{Parse, ParseStream};
use syn::Result;

use crate::Ctxt;

pub(crate) struct QuoteFn {
    pub(crate) stream: TokenStream,
}

impl Parse for QuoteFn {
    fn parse(input: ParseStream) -> Result<Self> {
        let cx = Ctxt::default();

        let parser = crate::quote::Quote::new(&cx);
        let (req, output) = parser.parse(input)?;

        let check = req.into_check(&cx.receiver);

        let Ctxt { receiver, module } = &cx;

        let stream = q::quote! {
            #module::tokens::from_fn(move |#receiver| {
                #output
                #check
            })
        };

        Ok(Self { stream })
    }
}