Skip to main content

defmt_macros/
lib.rs

1//! INTERNAL; DO NOT USE. Please use the `defmt` crate to access the functionality implemented here
2
3#![doc(html_logo_url = "https://knurling.ferrous-systems.com/knurling_logo_light_text.svg")]
4
5use defmt_parser::Level;
6use proc_macro::TokenStream;
7use proc_macro2::TokenStream as TokenStream2;
8use quote::quote;
9
10mod attributes;
11mod cargo;
12mod construct;
13mod consts;
14mod derives;
15mod function_like;
16mod items;
17
18// NOTE some proc-macro functions have an `_` (underscore) suffix. This is intentional.
19// If left unsuffixed the procedural macros would shadow macros defined in `std` (like `assert`)
20// within the context of this entire crate, which leads to lots of confusion.
21
22/* # Attributes */
23#[proc_macro_attribute]
24pub fn global_logger(args: TokenStream, item: TokenStream) -> TokenStream {
25    attributes::global_logger::expand(args, item)
26}
27
28#[proc_macro_attribute]
29pub fn panic_handler(args: TokenStream, item: TokenStream) -> TokenStream {
30    attributes::panic_handler::expand(args, item)
31}
32
33/* # Derives */
34#[proc_macro_derive(Format, attributes(defmt))]
35pub fn format(input: TokenStream) -> TokenStream {
36    derives::format::expand(input)
37}
38
39/* # Function-like */
40#[proc_macro]
41pub fn assert_(args: TokenStream) -> TokenStream {
42    function_like::assert_like::assert::expand(args)
43}
44
45#[proc_macro]
46pub fn assert_eq_(args: TokenStream) -> TokenStream {
47    function_like::assert_binop::eq(args)
48}
49
50#[proc_macro]
51pub fn assert_ne_(args: TokenStream) -> TokenStream {
52    function_like::assert_binop::ne(args)
53}
54
55/* ## `debug_` variants */
56// NOTE these `debug_*` macros can be written using `macro_rules!` (that'd be simpler) but that
57// results in an incorrect source code location being reported: the location of the `macro_rules!`
58// statement is reported. Using a proc-macro results in the call site being reported, which is what
59// we want
60#[proc_macro]
61pub fn debug_assert_(input: TokenStream) -> TokenStream {
62    let assert = TokenStream2::from(assert_(input));
63    quote!(if cfg!(debug_assertions) {
64        #assert
65    })
66    .into()
67}
68
69#[proc_macro]
70pub fn debug_assert_eq_(input: TokenStream) -> TokenStream {
71    let assert = TokenStream2::from(assert_eq_(input));
72    quote!(if cfg!(debug_assertions) {
73        #assert
74    })
75    .into()
76}
77
78#[proc_macro]
79pub fn debug_assert_ne_(input: TokenStream) -> TokenStream {
80    let assert = TokenStream2::from(assert_ne_(input));
81    quote!(if cfg!(debug_assertions) {
82        #assert
83    })
84    .into()
85}
86/* ## end of `debug_` variants */
87
88#[proc_macro]
89pub fn dbg(args: TokenStream) -> TokenStream {
90    function_like::dbg::expand(args)
91}
92
93#[proc_macro]
94pub fn intern(args: TokenStream) -> TokenStream {
95    function_like::intern::expand(args)
96}
97
98#[proc_macro]
99pub fn internp(args: TokenStream) -> TokenStream {
100    function_like::internp::expand(args)
101}
102
103#[proc_macro]
104pub fn println(args: TokenStream) -> TokenStream {
105    function_like::println::expand(args)
106}
107
108/* ## Logging macros */
109
110#[proc_macro]
111pub fn trace(args: TokenStream) -> TokenStream {
112    function_like::log::expand(Level::Trace, args)
113}
114
115#[proc_macro]
116pub fn debug(args: TokenStream) -> TokenStream {
117    function_like::log::expand(Level::Debug, args)
118}
119
120#[proc_macro]
121pub fn info(args: TokenStream) -> TokenStream {
122    function_like::log::expand(Level::Info, args)
123}
124
125#[proc_macro]
126pub fn warn(args: TokenStream) -> TokenStream {
127    function_like::log::expand(Level::Warn, args)
128}
129
130#[proc_macro]
131pub fn error(args: TokenStream) -> TokenStream {
132    function_like::log::expand(Level::Error, args)
133}
134/* ## end of logging macros */
135
136#[proc_macro]
137pub fn panic_(args: TokenStream) -> TokenStream {
138    function_like::panic_like::expand(args, "panicked at 'explicit panic'", |format_string| {
139        format!("panicked at '{format_string}'")
140    })
141}
142
143#[proc_macro]
144pub fn todo_(args: TokenStream) -> TokenStream {
145    function_like::panic_like::expand(args, "panicked at 'not yet implemented'", |format_string| {
146        format!("panicked at 'not yet implemented: {format_string}'")
147    })
148}
149
150#[proc_macro]
151pub fn unreachable_(args: TokenStream) -> TokenStream {
152    function_like::panic_like::expand(
153        args,
154        "panicked at 'internal error: entered unreachable code'",
155        |format_string| {
156            format!(
157                "panicked at 'internal error: entered unreachable code: {}'",
158                format_string
159            )
160        },
161    )
162}
163
164#[proc_macro]
165pub fn unwrap(args: TokenStream) -> TokenStream {
166    function_like::assert_like::unwrap::expand(args)
167}
168
169#[proc_macro]
170pub fn write(args: TokenStream) -> TokenStream {
171    function_like::write::expand(args)
172}
173
174/* # Items */
175#[proc_macro]
176pub fn bitflags(ts: TokenStream) -> TokenStream {
177    items::bitflags::expand(ts)
178}
179
180#[proc_macro]
181pub fn timestamp(args: TokenStream) -> TokenStream {
182    items::timestamp::expand(args)
183}