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
use defmt_parser::ParserMode;
use proc_macro::TokenStream;
use proc_macro_error::abort;
use quote::quote;
use syn::parse_macro_input;

use crate::{construct, function_like::log};

use self::args::Args;

mod args;

pub(crate) fn expand(args: TokenStream) -> TokenStream {
    let Args {
        formatter,
        log_args,
        ..
    } = parse_macro_input!(args as Args);

    let format_string = log_args.format_string.value();
    let fragments = match defmt_parser::parse(&format_string, ParserMode::Strict) {
        Ok(args) => args,
        Err(e) => abort!(log_args.format_string, "{}", e),
    };

    let formatting_exprs: Vec<_> = log_args
        .formatting_args
        .map(|punctuated| punctuated.into_iter().collect())
        .unwrap_or_default();

    let log::Codegen { patterns, exprs } = log::Codegen::new(
        &fragments,
        formatting_exprs.len(),
        log_args.format_string.span(),
    );

    let format_tag = construct::interned_string(&format_string, "write", false);
    quote!({
        let _typecheck_formatter: defmt::Formatter<'_> = #formatter;
        match (#(&(#formatting_exprs)),*) {
            (#(#patterns),*) => {
                defmt::export::istr(&#format_tag);
                #(#exprs;)*
            }
        }
    })
    .into()
}