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