emit_macros/lib.rs
1/*!
2Implementation details for `emit!` macros.
3
4This crate is not intended to be consumed directly.
5*/
6
7/*
8# Organization
9
10This crate contains the proc-macros that are exported in the `emit` crate. It expands to code that uses the `emit::__private` API, in particular the `emit::macro_hooks` module.
11
12# Hooks
13
14Code is transformed through _hooks_. A hook is a well-known method call, like `a.__private_emit_capture_as_default()`. The behavior of the hook is defined in `emit::macro_hooks`. Attribute macros look for these hooks and replace them to change behavior. For example, `#[emit::as_debug]` looks for any `__private_emit_capture_as_*` method and replaces it with `__private_emit_capture_as_debug`.
15
16# Testing
17
18Tests for this project mostly live in the top-level `test/ui` crate.
19*/
20
21#![deny(missing_docs)]
22#![doc(html_logo_url = "https://raw.githubusercontent.com/emit-rs/emit/main/asset/logo.svg")]
23
24extern crate proc_macro;
25
26#[macro_use]
27extern crate quote;
28
29#[macro_use]
30extern crate syn;
31
32use std::collections::HashMap;
33
34use proc_macro2::TokenStream;
35
36mod args;
37mod build;
38mod capture;
39mod dbg;
40mod emit;
41mod fmt;
42mod format;
43mod hook;
44mod key;
45mod optional;
46mod props;
47mod span;
48mod template;
49mod util;
50
51use util::ResultToTokens;
52
53/**
54The set of hooks defined as a map.
55
56Hooks are regular attribute macros, but will be eagerly applied when expanding other macros to avoid the nightly-only feature that allows attribute macros on expressions. The `hook::eval_hooks` function will do this expansion.
57*/
58fn hooks() -> HashMap<&'static str, fn(TokenStream, TokenStream) -> syn::Result<TokenStream>> {
59 let mut map = HashMap::new();
60
61 map.insert(
62 "fmt",
63 (|args: TokenStream, expr: TokenStream| {
64 fmt::rename_hook_tokens(fmt::RenameHookTokens { args, expr })
65 }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
66 );
67
68 map.insert(
69 "key",
70 (|args: TokenStream, expr: TokenStream| {
71 key::rename_hook_tokens(key::RenameHookTokens { args, expr })
72 }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
73 );
74
75 map.insert(
76 "optional",
77 (|args: TokenStream, expr: TokenStream| {
78 optional::rename_hook_tokens(optional::RenameHookTokens { args, expr })
79 }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
80 );
81
82 map.insert(
83 "as_value",
84 (|args: TokenStream, expr: TokenStream| {
85 capture_as(
86 "as_value",
87 args,
88 expr,
89 quote!(__private_capture_as_value),
90 quote!(__private_capture_anon_as_value),
91 )
92 }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
93 );
94
95 map.insert(
96 "as_debug",
97 (|args: TokenStream, expr: TokenStream| {
98 capture_as(
99 "as_debug",
100 args,
101 expr,
102 quote!(__private_capture_as_debug),
103 quote!(__private_capture_anon_as_debug),
104 )
105 }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
106 );
107
108 map.insert(
109 "as_display",
110 (|args: TokenStream, expr: TokenStream| {
111 capture_as(
112 "as_display",
113 args,
114 expr,
115 quote!(__private_capture_as_display),
116 quote!(__private_capture_anon_as_display),
117 )
118 }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
119 );
120
121 map.insert(
122 "as_sval",
123 (|args: TokenStream, expr: TokenStream| {
124 #[cfg(feature = "sval")]
125 {
126 capture_as(
127 "as_sval",
128 args,
129 expr,
130 quote!(__private_capture_as_sval),
131 quote!(__private_capture_anon_as_sval),
132 )
133 }
134 #[cfg(not(feature = "sval"))]
135 {
136 use syn::spanned::Spanned;
137
138 let _ = args;
139
140 Err(syn::Error::new(expr.span(), "capturing with `sval` is only possible when the `sval` Cargo feature is enabled"))
141 }
142 }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>
143 );
144
145 map.insert(
146 "as_serde",
147 (|args: TokenStream, expr: TokenStream| {
148 #[cfg(feature = "serde")]
149 {
150 capture_as(
151 "as_serde",
152 args,
153 expr,
154 quote!(__private_capture_as_serde),
155 quote!(__private_capture_anon_as_serde),
156 )
157 }
158 #[cfg(not(feature = "serde"))]
159 {
160 use syn::spanned::Spanned;
161
162 let _ = args;
163
164 Err(syn::Error::new(expr.span(), "capturing with `serde` is only possible when the `serde` Cargo feature is enabled"))
165 }
166 }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>
167 );
168
169 map.insert(
170 "as_error",
171 (|args: TokenStream, expr: TokenStream| {
172 #[cfg(feature = "std")]
173 {
174 capture_as(
175 "as_error",
176 args,
177 expr,
178 quote!(__private_capture_as_error),
179 quote!(__private_capture_as_error),
180 )
181 }
182 #[cfg(not(feature = "std"))]
183 {
184 use syn::spanned::Spanned;
185
186 let _ = args;
187
188 Err(syn::Error::new(
189 expr.span(),
190 "capturing errors is only possible when the `std` Cargo feature is enabled",
191 ))
192 }
193 }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
194 );
195
196 map
197}
198
199/**
200Format a template.
201
202# Syntax
203
204See the [`macro@emit`] macro for syntax.
205
206# Control parameters
207
208This macro doesn't accept any control parameters.
209
210# Returns
211
212A `String`.
213*/
214#[proc_macro]
215pub fn format(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
216 format::expand_tokens(format::ExpandTokens {
217 input: TokenStream::from(item),
218 })
219 .unwrap_or_compile_error()
220}
221
222/**
223Construct an event.
224
225# Syntax
226
227```text
228(control_param),* tpl, (property),*
229```
230
231where
232
233- `control_param`: A Rust field-value with a pre-determined identifier (see below).
234- `tpl`: A template string literal.
235- `property`: A Rust field-value for a property to capture.
236
237# Control parameters
238
239This macro accepts the following optional control parameters:
240
241| name | type | description |
242| -------- | ----------------------- | -------------------------------------------------------------------------------- |
243| `mdl` | `impl Into<emit::Path>` | The module the event belongs to. If unspecified the current module path is used. |
244| `props` | `impl emit::Props` | A base set of properties to add to the event. |
245| `extent` | `impl emit::ToExtent` | The extent to use on the event. |
246
247# Template
248
249The template for the event. See the [`macro@tpl`] macro for syntax.
250
251# Properties
252
253Properties that appear within the template or after it are added to the emitted event. The identifier of the property is its key. Property capturing can be adjusted through the `as_*` attribute macros.
254
255# Returns
256
257An `emit::Event`.
258*/
259#[proc_macro]
260pub fn evt(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
261 build::expand_evt_tokens(build::ExpandEvtTokens {
262 level: None,
263 input: item.into(),
264 })
265 .unwrap_or_compile_error()
266}
267
268/**
269Construct a debug event.
270
271# Syntax
272
273See the [`macro@evt`] macro for syntax.
274
275# Returns
276
277An `emit::Event`.
278*/
279#[proc_macro]
280pub fn debug_evt(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
281 build::expand_evt_tokens(build::ExpandEvtTokens {
282 level: Some(quote!(emit::Level::Debug)),
283 input: item.into(),
284 })
285 .unwrap_or_compile_error()
286}
287
288/**
289Construct an info event.
290
291# Syntax
292
293See the [`macro@evt`] macro for syntax.
294
295# Returns
296
297An `emit::Event`.
298*/
299#[proc_macro]
300pub fn info_evt(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
301 build::expand_evt_tokens(build::ExpandEvtTokens {
302 level: Some(quote!(emit::Level::Info)),
303 input: item.into(),
304 })
305 .unwrap_or_compile_error()
306}
307
308/**
309Construct a warn event.
310
311# Syntax
312
313See the [`macro@evt`] macro for syntax.
314
315# Returns
316
317An `emit::Event`.
318*/
319#[proc_macro]
320pub fn warn_evt(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
321 build::expand_evt_tokens(build::ExpandEvtTokens {
322 level: Some(quote!(emit::Level::Warn)),
323 input: item.into(),
324 })
325 .unwrap_or_compile_error()
326}
327
328/**
329Construct an error event.
330
331# Syntax
332
333See the [`macro@evt`] macro for syntax.
334
335# Returns
336
337An `emit::Event`.
338*/
339#[proc_macro]
340pub fn error_evt(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
341 build::expand_evt_tokens(build::ExpandEvtTokens {
342 level: Some(quote!(emit::Level::Error)),
343 input: item.into(),
344 })
345 .unwrap_or_compile_error()
346}
347
348/**
349Wrap an operation in a span.
350
351# Syntax
352
353```text
354(control_param),* tpl, (property),*
355```
356
357where
358
359- `control_param`: A Rust field-value with a pre-determined identifier (see below).
360- `tpl`: A template string literal.
361- `property`: A Rust field-value for a property to capture.
362
363# Control parameters
364
365This macro accepts the following optional control parameters:
366
367| name | type | description |
368| ----------- | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
369| `rt` | `impl emit::runtime::Runtime` | The runtime to emit the event through. |
370| `mdl` | `impl Into<emit::Path>` | The module the event belongs to. If unspecified the current module path is used. |
371| `when` | `impl emit::Filter` | A filter to use instead of the one configured on the runtime. |
372| `guard` | - | An identifier to bind an `emit::SpanGuard` to in the body of the span for manual completion. |
373| `ok_lvl` | `str` or `emit::Level` | Assume the instrumented block returns a `Result`. Assign the event the given level when the result is `Ok`. |
374| `err_lvl` | `str` or `emit::Level` | Assume the instrumented block returns a `Result`. Assign the event the given level when the result is `Err` and attach the error as the `err` property. |
375| `panic_lvl` | `str` or `emit::Level` | Detect whether the function panics and use the given level if it does. |
376| `err` | `impl Fn(&E) -> T` | Assume the instrumented block returns a `Result`. Map the `Err` variant into a new type `T` that is `str`, `&(dyn Error + 'static)`, or `impl Error + 'static` |
377| `setup` | `impl Fn() -> T` | Invoke the expression before creating the span, binding the result to a value that's dropped at the end of the annotated function. |
378
379# Template
380
381The template for the event. See the [`macro@tpl`] macro for syntax.
382
383# Properties
384
385Properties that appear within the template or after it are added to the emitted event. The identifier of the property is its key. Property capturing can be adjusted through the `as_*` attribute macros.
386*/
387#[proc_macro_attribute]
388pub fn span(
389 args: proc_macro::TokenStream,
390 item: proc_macro::TokenStream,
391) -> proc_macro::TokenStream {
392 span::expand_tokens(span::ExpandTokens {
393 level: None,
394 input: TokenStream::from(args),
395 item: TokenStream::from(item),
396 })
397 .unwrap_or_compile_error()
398}
399
400/**
401Wrap an operation in a debug span.
402
403# Syntax
404
405See the [`macro@span`] macro for syntax.
406*/
407#[proc_macro_attribute]
408pub fn debug_span(
409 args: proc_macro::TokenStream,
410 item: proc_macro::TokenStream,
411) -> proc_macro::TokenStream {
412 span::expand_tokens(span::ExpandTokens {
413 level: Some(quote!(emit::Level::Debug)),
414 input: TokenStream::from(args),
415 item: TokenStream::from(item),
416 })
417 .unwrap_or_compile_error()
418}
419
420/**
421Wrap an operation in an info span.
422
423# Syntax
424
425See the [`macro@span`] macro for syntax.
426*/
427#[proc_macro_attribute]
428pub fn info_span(
429 args: proc_macro::TokenStream,
430 item: proc_macro::TokenStream,
431) -> proc_macro::TokenStream {
432 span::expand_tokens(span::ExpandTokens {
433 level: Some(quote!(emit::Level::Info)),
434 input: TokenStream::from(args),
435 item: TokenStream::from(item),
436 })
437 .unwrap_or_compile_error()
438}
439
440/**
441Wrap an operation in a warn span.
442
443# Syntax
444
445See the [`macro@span`] macro for syntax.
446*/
447#[proc_macro_attribute]
448pub fn warn_span(
449 args: proc_macro::TokenStream,
450 item: proc_macro::TokenStream,
451) -> proc_macro::TokenStream {
452 span::expand_tokens(span::ExpandTokens {
453 level: Some(quote!(emit::Level::Warn)),
454 input: TokenStream::from(args),
455 item: TokenStream::from(item),
456 })
457 .unwrap_or_compile_error()
458}
459
460/**
461Wrap an operation in an error span.
462
463# Syntax
464
465See the [`macro@span`] macro for syntax.
466*/
467#[proc_macro_attribute]
468pub fn error_span(
469 args: proc_macro::TokenStream,
470 item: proc_macro::TokenStream,
471) -> proc_macro::TokenStream {
472 span::expand_tokens(span::ExpandTokens {
473 level: Some(quote!(emit::Level::Error)),
474 input: TokenStream::from(args),
475 item: TokenStream::from(item),
476 })
477 .unwrap_or_compile_error()
478}
479
480/**
481Start a span.
482
483See the [`SpanGuard::new`](https://docs.rs/emit/1.8.0/emit/span/struct.SpanGuard.html#method.new) for details on starting and completing the returned span.
484
485# Syntax
486
487```text
488(control_param),* tpl, (property),*
489```
490
491where
492
493- `control_param`: A Rust field-value with a pre-determined identifier (see below).
494- `tpl`: A template string literal.
495- `property`: A Rust field-value for a property to capture.
496
497# Control parameters
498
499This macro accepts the following optional control parameters:
500
501| name | type | description |
502| ----------- | ----------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
503| `rt` | `impl emit::runtime::Runtime` | The runtime to emit the event through. |
504| `mdl` | `impl Into<emit::Path>` | The module the event belongs to. If unspecified the current module path is used. |
505| `when` | `impl emit::Filter` | A filter to use instead of the one configured on the runtime. |
506| `panic_lvl` | `str` or `emit::Level` | Detect whether the function panics and use the given level if it does. |
507
508# Template
509
510The template for the event. See the [`macro@tpl`] macro for syntax.
511
512# Properties
513
514Properties that appear within the template or after it are added to the emitted event. The identifier of the property is its key. Property capturing can be adjusted through the `as_*` attribute macros.
515*/
516#[proc_macro]
517pub fn new_span(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
518 span::expand_new_tokens(span::ExpandNewTokens {
519 level: None,
520 input: TokenStream::from(item),
521 })
522 .unwrap_or_compile_error()
523}
524
525/**
526Start a debug span.
527
528# Syntax
529
530See the [`macro@new_span`] macro for syntax.
531*/
532#[proc_macro]
533pub fn new_debug_span(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
534 span::expand_new_tokens(span::ExpandNewTokens {
535 level: Some(quote!(emit::Level::Debug)),
536 input: TokenStream::from(item),
537 })
538 .unwrap_or_compile_error()
539}
540
541/**
542Start an info span.
543
544# Syntax
545
546See the [`macro@new_span`] macro for syntax.
547*/
548#[proc_macro]
549pub fn new_info_span(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
550 span::expand_new_tokens(span::ExpandNewTokens {
551 level: Some(quote!(emit::Level::Info)),
552 input: TokenStream::from(item),
553 })
554 .unwrap_or_compile_error()
555}
556
557/**
558Start a warning span.
559
560# Syntax
561
562See the [`macro@new_span`] macro for syntax.
563*/
564#[proc_macro]
565pub fn new_warn_span(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
566 span::expand_new_tokens(span::ExpandNewTokens {
567 level: Some(quote!(emit::Level::Warn)),
568 input: TokenStream::from(item),
569 })
570 .unwrap_or_compile_error()
571}
572
573/**
574Start an error span.
575
576# Syntax
577
578See the [`macro@new_span`] macro for syntax.
579*/
580#[proc_macro]
581pub fn new_error_span(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
582 span::expand_new_tokens(span::ExpandNewTokens {
583 level: Some(quote!(emit::Level::Error)),
584 input: TokenStream::from(item),
585 })
586 .unwrap_or_compile_error()
587}
588
589/**
590Construct a template.
591
592Templates are text literals that include regular text with _holes_. A hole is a point in the template where a property should be interpolated in.
593
594# Syntax
595
596```text
597template_literal
598```
599
600where
601
602- `template_literal`: `"` `(text | hole)*` `"`
603- `text`: A fragment of plain text where `{` are escaped as `{{` and `}` are escaped as `}}`.
604- `hole`: `{` `property` `}`
605- `property`: A Rust field-value expression.
606
607The following are all examples of templates:
608
609```text
610"some text"
611 ├───────┘
612 text
613```
614
615```text
616"some text and {x}"
617 ├────────────┘ │
618 text property
619```
620
621```text
622"some {{text}} and {x: 42} and {y}"
623 ├────────────────┘ ├───┘ └───┤ │
624 text property │ property
625 text
626```
627
628See [the guide](https://emit-rs.io/reference/templates.html) for more details and examples of templates.
629
630# Returns
631
632An `emit::Template`.
633*/
634#[proc_macro]
635pub fn tpl(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
636 build::expand_tpl_tokens(build::ExpandTplTokens {
637 input: TokenStream::from(item),
638 })
639 .unwrap_or_compile_error()
640}
641
642/**
643Emit an event.
644
645# Syntax
646
647```text
648(control_param),* tpl, (property),*
649```
650
651where
652
653- `control_param`: A Rust field-value with a pre-determined identifier (see below).
654- `tpl`: A template string literal.
655- `property`: A Rust field-value for a property to capture.
656
657# Control parameters
658
659This macro accepts the following optional control parameters:
660
661| name | type | description |
662| --------- | ----------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
663| `rt` | `impl emit::runtime::Runtime` | The runtime to emit the event through. |
664| `mdl` | `impl Into<emit::Path>` | The module the event belongs to. If unspecified the current module path is used. |
665| `extent` | `impl emit::ToExtent` | The extent to use on the event. If it resolves to `None` then the clock on the runtime will be used to assign a point extent. |
666| `props` | `impl emit::Props` | A base set of properties to add to the event. |
667| `evt` | `impl emit::event::ToEvent` | A base event to emit. Any properties captured by the macro will be appended to the base event. If this control parameter is specified then `mdl`, `props`, and `extent` cannot also be set. |
668| `when` | `impl emit::Filter` | A filter to use instead of the one configured on the runtime. |
669
670# Template
671
672The template for the event. See the [`macro@tpl`] macro for syntax.
673
674# Properties
675
676Properties that appear within the template or after it are added to the emitted event. The identifier of the property is its key. Property capturing can be adjusted through the `as_*` attribute macros.
677*/
678#[proc_macro]
679pub fn emit(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
680 emit::expand_tokens(emit::ExpandTokens {
681 level: None,
682 input: TokenStream::from(item),
683 })
684 .unwrap_or_compile_error()
685}
686
687/**
688Emit a debug event.
689
690# Syntax
691
692See the [`macro@emit`] macro for syntax.
693*/
694#[proc_macro]
695pub fn debug(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
696 emit::expand_tokens(emit::ExpandTokens {
697 level: Some(quote!(emit::Level::Debug)),
698 input: TokenStream::from(item),
699 })
700 .unwrap_or_compile_error()
701}
702
703/**
704Emit an info event.
705
706# Syntax
707
708See the [`macro@emit`] macro for syntax.
709*/
710#[proc_macro]
711pub fn info(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
712 emit::expand_tokens(emit::ExpandTokens {
713 level: Some(quote!(emit::Level::Info)),
714 input: TokenStream::from(item),
715 })
716 .unwrap_or_compile_error()
717}
718
719/**
720Emit a warn event.
721
722# Syntax
723
724See the [`macro@emit`] macro for syntax.
725*/
726#[proc_macro]
727pub fn warn(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
728 emit::expand_tokens(emit::ExpandTokens {
729 level: Some(quote!(emit::Level::Warn)),
730 input: TokenStream::from(item),
731 })
732 .unwrap_or_compile_error()
733}
734
735/**
736Emit an error event.
737
738# Syntax
739
740See the [`macro@emit`] macro for syntax.
741*/
742#[proc_macro]
743pub fn error(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
744 emit::expand_tokens(emit::ExpandTokens {
745 level: Some(quote!(emit::Level::Error)),
746 input: TokenStream::from(item),
747 })
748 .unwrap_or_compile_error()
749}
750
751/**
752Emit a temporary debug event.
753
754# Syntax
755
756```text
757(property),*
758tpl, (property),*
759```
760
761where
762
763- `tpl`: A template string literal.
764- `property`: A Rust field-value for a property to capture.
765
766# Properties
767
768Properties that appear within the template or after it are added to the emitted event. The identifier of the property is its key. Property capturing can be adjusted through the `as_*` attribute macros.
769
770Unlike [`macro@debug`], this macro captures values using their [`Debug`](https://doc.rust-lang.org/std/fmt/trait.Debug.html) implementation by default.
771
772# When to use `dbg`
773
774This macro is a convenient way to pepper debug logs through code, but follows the same recommendations as the standard library's `dbg` macro.
775You shouldn't expect `dbg` statements to be long lived, and use the [`macro@debug`] macro instead with more deliberate data.
776*/
777#[proc_macro]
778pub fn dbg(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
779 dbg::expand_tokens(dbg::ExpandTokens {
780 input: TokenStream::from(item),
781 })
782 .unwrap_or_compile_error()
783}
784
785/**
786Construct a path.
787
788# Syntax
789
790```text
791path
792```
793
794where
795
796- `path`: A string literal containing a valid `emit` path.
797*/
798#[proc_macro]
799pub fn path(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
800 build::expand_path_tokens(build::ExpandPathTokens {
801 input: TokenStream::from(item),
802 })
803 .unwrap_or_compile_error()
804}
805
806/**
807Construct a set of properties.
808
809# Syntax
810
811```text
812(property),*
813```
814
815where
816
817- `property`: A Rust field-value for a property. The identifier of the field-value is the key of the property.
818*/
819#[proc_macro]
820pub fn props(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
821 build::expand_props_tokens(build::ExpandPropsTokens {
822 input: TokenStream::from(item),
823 })
824 .unwrap_or_compile_error()
825}
826
827/**
828Specify Rust format flags to use when rendering a property in a template.
829
830# Syntax
831
832```text
833fmt_string
834```
835
836where
837
838- `fmt_string`: A string literal with the format flags, like `":?"`. See the [`std::fmt`](https://doc.rust-lang.org/std/fmt/index.html) docs for details on available flags.
839
840# Applicable to
841
842This attribute can be applied to properties that appear in a template.
843*/
844#[proc_macro_attribute]
845pub fn fmt(
846 args: proc_macro::TokenStream,
847 item: proc_macro::TokenStream,
848) -> proc_macro::TokenStream {
849 (hook::get("fmt").unwrap())(TokenStream::from(args), TokenStream::from(item))
850 .unwrap_or_compile_error()
851}
852
853/**
854Specify the key for a property.
855
856# Syntax
857
858```text
859key
860```
861
862where
863
864- `key`: A string literal with the key to use. The key doesn't need to be a valid Rust identifier.
865
866# Applicable to
867
868This attribute can be applied to properties.
869*/
870#[proc_macro_attribute]
871pub fn key(
872 args: proc_macro::TokenStream,
873 item: proc_macro::TokenStream,
874) -> proc_macro::TokenStream {
875 (hook::get("key").unwrap())(TokenStream::from(args), TokenStream::from(item))
876 .unwrap_or_compile_error()
877}
878
879/**
880Specify that a property value of `None` should not be captured, instead of being captured as `null`.
881
882# Syntax
883
884This macro doesn't accept any arguments.
885
886# Applicable to
887
888This attribute can be applied to properties where the type is `Option<&T>`.
889*/
890#[proc_macro_attribute]
891pub fn optional(
892 args: proc_macro::TokenStream,
893 item: proc_macro::TokenStream,
894) -> proc_macro::TokenStream {
895 (hook::get("optional").unwrap())(TokenStream::from(args), TokenStream::from(item))
896 .unwrap_or_compile_error()
897}
898
899/**
900Capture a property using its `ToValue` implementation.
901
902# Syntax
903
904This macro doesn't accept any arguments.
905
906# Applicable to
907
908This attribute can be applied to properties.
909*/
910#[proc_macro_attribute]
911pub fn as_value(
912 args: proc_macro::TokenStream,
913 item: proc_macro::TokenStream,
914) -> proc_macro::TokenStream {
915 (hook::get("as_value").unwrap())(TokenStream::from(args), TokenStream::from(item))
916 .unwrap_or_compile_error()
917}
918
919/**
920Capture a property using its `Debug` implementation.
921
922# Syntax
923
924This macro doesn't accept any arguments.
925
926# Applicable to
927
928This attribute can be applied to properties.
929*/
930#[proc_macro_attribute]
931pub fn as_debug(
932 args: proc_macro::TokenStream,
933 item: proc_macro::TokenStream,
934) -> proc_macro::TokenStream {
935 (hook::get("as_debug").unwrap())(TokenStream::from(args), TokenStream::from(item))
936 .unwrap_or_compile_error()
937}
938
939/**
940Capture a property using its `Display` implementation.
941
942# Syntax
943
944This macro doesn't accept any arguments.
945
946# Applicable to
947
948This attribute can be applied to properties.
949*/
950#[proc_macro_attribute]
951pub fn as_display(
952 args: proc_macro::TokenStream,
953 item: proc_macro::TokenStream,
954) -> proc_macro::TokenStream {
955 (hook::get("as_display").unwrap())(TokenStream::from(args), TokenStream::from(item))
956 .unwrap_or_compile_error()
957}
958
959/**
960Capture a property using its `sval::Value` implementation.
961
962# Syntax
963
964This macro doesn't accept any arguments.
965
966# Applicable to
967
968This attribute can be applied to properties.
969*/
970#[proc_macro_attribute]
971pub fn as_sval(
972 args: proc_macro::TokenStream,
973 item: proc_macro::TokenStream,
974) -> proc_macro::TokenStream {
975 (hook::get("as_sval").unwrap())(TokenStream::from(args), TokenStream::from(item))
976 .unwrap_or_compile_error()
977}
978
979/**
980Capture a property using its `serde::Serialize` implementation.
981
982# Syntax
983
984This macro doesn't accept any arguments.
985
986# Applicable to
987
988This attribute can be applied to properties.
989*/
990#[proc_macro_attribute]
991pub fn as_serde(
992 args: proc_macro::TokenStream,
993 item: proc_macro::TokenStream,
994) -> proc_macro::TokenStream {
995 (hook::get("as_serde").unwrap())(TokenStream::from(args), TokenStream::from(item))
996 .unwrap_or_compile_error()
997}
998
999/**
1000Capture a property using its `Error` implementation.
1001
1002# Syntax
1003
1004This macro doesn't accept any arguments.
1005
1006# Applicable to
1007
1008This attribute can be applied to properties.
1009*/
1010#[proc_macro_attribute]
1011pub fn as_error(
1012 args: proc_macro::TokenStream,
1013 item: proc_macro::TokenStream,
1014) -> proc_macro::TokenStream {
1015 (hook::get("as_error").unwrap())(TokenStream::from(args), TokenStream::from(item))
1016 .unwrap_or_compile_error()
1017}
1018
1019/*
1020fn base_emit(level: Option<TokenStream>, item: TokenStream) -> proc_macro::TokenStream {
1021 emit::expand_tokens(emit::ExpandTokens { level, input: item }).unwrap_or_compile_error()
1022}
1023
1024fn base_span(
1025 level: Option<TokenStream>,
1026 input: TokenStream,
1027 item: TokenStream,
1028) -> proc_macro::TokenStream {
1029 if filter::matches_build_filter() {
1030 span::expand_tokens(span::ExpandTokens { level, input, item }).unwrap_or_compile_error()
1031 } else {
1032 item.into()
1033 }
1034}
1035*/
1036
1037fn capture_as(
1038 name: &'static str,
1039 args: TokenStream,
1040 expr: TokenStream,
1041 as_fn: TokenStream,
1042 as_anon_fn: TokenStream,
1043) -> syn::Result<TokenStream> {
1044 capture::rename_hook_tokens(capture::RenameHookTokens {
1045 name,
1046 args,
1047 expr,
1048 to: |args: &capture::Args| {
1049 if args.inspect {
1050 as_fn.clone()
1051 } else {
1052 as_anon_fn.clone()
1053 }
1054 },
1055 })
1056}