1 | use proc_macro::TokenStream; |
2 | use quote::quote; |
3 | use syn::parse_macro_input; |
4 | |
5 | use crate::construct; |
6 | |
7 | use self::args::Args; |
8 | |
9 | mod args; |
10 | |
11 | pub(crate) fn expand(args: TokenStream) -> TokenStream { |
12 | let args: Args = parse_macro_input!(args as Args); |
13 | codegen(&args) |
14 | } |
15 | |
16 | fn codegen(args: &Args) -> TokenStream { |
17 | let tuple_exprs = args |
18 | .exprs |
19 | .iter() |
20 | .map(|expr| { |
21 | let escaped_expr = construct::escaped_expr_string(expr); |
22 | let format_string = format!(" {escaped_expr} = {{}}" ); |
23 | |
24 | quote!(match #expr { |
25 | tmp => { |
26 | defmt::trace!(#format_string, tmp); |
27 | tmp |
28 | } |
29 | }) |
30 | }) |
31 | .collect::<Vec<_>>(); |
32 | |
33 | if tuple_exprs.is_empty() { |
34 | // for compatibility with `std::dbg!` we also emit a TRACE log in this case |
35 | quote!(defmt::trace!("" )) |
36 | } else { |
37 | quote![ (#(#tuple_exprs),*) ] |
38 | } |
39 | .into() |
40 | } |
41 | |