| 1 |
use std::borrow::Cow; |
| 2 |
|
| 3 |
use defmt_parser::Level; |
| 4 |
use proc_macro::TokenStream; |
| 5 |
use quote::quote; |
| 6 |
use syn::parse_macro_input; |
| 7 |
|
| 8 |
use crate::{construct, function_like::log}; |
| 9 |
|
| 10 |
pub(crate) fn expand( |
| 11 |
args: TokenStream, |
| 12 |
zero_args_format_string: &str, |
| 13 |
transform_format_string: impl FnOnce(&str) -> String, |
| 14 |
) -> TokenStream { |
| 15 |
let (format_string, formatting_args) = if args.is_empty() { |
| 16 |
// panic!() -> error!("panicked at 'explicit panic'") |
| 17 |
(Cow::from(zero_args_format_string), None) |
| 18 |
} else { |
| 19 |
// panic!("a", b, c) -> error!("panicked at 'a'", b, c) |
| 20 |
let log_args = parse_macro_input!(args as log::Args); |
| 21 |
let format_string = transform_format_string(&log_args.format_string.value()); |
| 22 |
|
| 23 |
(Cow::from(format_string), log_args.formatting_args) |
| 24 |
}; |
| 25 |
|
| 26 |
let format_string = construct::string_literal(&format_string); |
| 27 |
let log_stmt = match log::expand_parsed( |
| 28 |
Level::Error, |
| 29 |
log::Args { |
| 30 |
format_string, |
| 31 |
formatting_args, |
| 32 |
}, |
| 33 |
) { |
| 34 |
Ok(log_stmt) => log_stmt, |
| 35 |
Err(err) => return err.into_compile_error().into(), |
| 36 |
}; |
| 37 |
|
| 38 |
quote!( |
| 39 |
{ |
| 40 |
#log_stmt; |
| 41 |
defmt::export::panic() |
| 42 |
} |
| 43 |
) |
| 44 |
.into() |
| 45 |
} |