l0g_macros/
lib.rs

1//! proc-macro crate intended to be used together with `l0g` crate.
2
3#![warn(missing_docs)]
4
5use proc_macro::TokenStream;
6use proc_macro_error::proc_macro_error;
7
8#[cfg(all(feature = "defmt", feature = "log"))]
9compile_error!("defmt and log are mutually exclusive");
10
11/// Derive the appropriate "formatting" trait, depending on the feature chosen
12/// on the `l0g` crate.
13/// - `defmt` will derive `defmt::Format` trait
14/// - `log` will derive `core::fmt::Debug` trait
15/// - no feature is a noop
16#[cfg(feature = "defmt")]
17#[proc_macro_attribute]
18#[proc_macro_error]
19pub fn format(_: TokenStream, input: TokenStream) -> TokenStream {
20    let input = syn::parse_macro_input!(input as syn::DeriveInput);
21    quote::quote!(
22        #[allow(unused)]
23        use l0g::__internal::*;
24        #[derive(defmt::Format)]
25        #input
26    )
27    .into()
28}
29
30/// Derive the appropriate "formatting" trait, depending on the feature chosen
31/// on the `l0g` crate.
32/// - `defmt` will derive `defmt::Format` trait
33/// - `log` will derive `core::fmt::Debug` trait
34/// - no feature is a noop
35#[cfg(feature = "log")]
36#[proc_macro_attribute]
37#[proc_macro_error]
38pub fn format(_: TokenStream, input: TokenStream) -> TokenStream {
39    let input = syn::parse_macro_input!(input as syn::DeriveInput);
40    quote::quote!(
41        #[derive(core::fmt::Debug)]
42        #input
43    )
44    .into()
45}
46
47/// Derive the appropriate "formatting" trait, depending on the feature chosen
48/// on the `l0g` crate.
49/// - `defmt` will derive `defmt::Format` trait
50/// - `log` will derive `core::fmt::Debug` trait
51/// - no feature is a noop
52#[cfg(all(not(feature = "defmt"), not(feature = "log")))]
53#[proc_macro_attribute]
54#[proc_macro_error]
55pub fn format(_: TokenStream, input: TokenStream) -> TokenStream {
56    input
57}