1 | use defmt_parser::Level; |
2 | use proc_macro::TokenStream; |
3 | use quote::quote; |
4 | use syn::parse_macro_input; |
5 | |
6 | use crate::{construct, function_like::log}; |
7 | |
8 | pub(crate) fn expand(args: TokenStream) -> TokenStream { |
9 | let args = parse_macro_input!(args as super::Args); |
10 | |
11 | let condition = args.condition; |
12 | let (format_string, formatting_args) = if let Some(log_args) = args.log_args { |
13 | let format_string = format!("panicked at ' {}'" , log_args.format_string.value()); |
14 | (format_string, log_args.formatting_args) |
15 | } else { |
16 | let format_string = format!( |
17 | "panicked at 'assertion failed: {}'" , |
18 | construct::escaped_expr_string(&condition) |
19 | ); |
20 | (format_string, None) |
21 | }; |
22 | |
23 | let format_string = construct::string_literal(&format_string); |
24 | let log_stmt = log::expand_parsed( |
25 | Level::Error, |
26 | log::Args { |
27 | format_string, |
28 | formatting_args, |
29 | }, |
30 | ); |
31 | |
32 | quote!( |
33 | if !(#condition) { |
34 | #log_stmt; |
35 | defmt::export::panic() |
36 | } |
37 | ) |
38 | .into() |
39 | } |
40 | |