1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
/*!
Implementation details for `emit!` macros.

This crate is not intended to be consumed directly.
*/

#![deny(missing_docs)]

/*
# Organization

This 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.

# Hooks

Code 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`.
*/

#![doc(html_logo_url = "https://raw.githubusercontent.com/KodrAus/emit/main/asset/logo.svg")]

extern crate proc_macro;

#[macro_use]
extern crate quote;

#[macro_use]
extern crate syn;

use std::collections::HashMap;

use proc_macro2::TokenStream;

mod args;
mod build;
mod capture;
mod emit;
mod event;
mod filter;
mod fmt;
mod format;
mod hook;
mod key;
mod module;
mod optional;
mod props;
mod span;
mod template;
mod time;
mod util;

use util::ResultToTokens;

fn hooks() -> HashMap<&'static str, fn(TokenStream, TokenStream) -> syn::Result<TokenStream>> {
    let mut map = HashMap::new();

    map.insert(
        "fmt",
        (|args: TokenStream, expr: TokenStream| {
            fmt::rename_hook_tokens(fmt::RenameHookTokens { args, expr })
        }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
    );

    map.insert(
        "key",
        (|args: TokenStream, expr: TokenStream| {
            key::rename_hook_tokens(key::RenameHookTokens { args, expr })
        }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
    );

    map.insert(
        "optional",
        (|args: TokenStream, expr: TokenStream| {
            optional::rename_hook_tokens(optional::RenameHookTokens { args, expr })
        }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
    );

    map.insert(
        "as_value",
        (|args: TokenStream, expr: TokenStream| {
            capture_as(
                "as_value",
                args,
                expr,
                quote!(__private_capture_as_value),
                quote!(__private_capture_anon_as_value),
            )
        }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
    );

    map.insert(
        "as_debug",
        (|args: TokenStream, expr: TokenStream| {
            capture_as(
                "as_debug",
                args,
                expr,
                quote!(__private_capture_as_debug),
                quote!(__private_capture_anon_as_debug),
            )
        }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
    );

    map.insert(
        "as_display",
        (|args: TokenStream, expr: TokenStream| {
            capture_as(
                "as_display",
                args,
                expr,
                quote!(__private_capture_as_display),
                quote!(__private_capture_anon_as_display),
            )
        }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
    );

    map.insert(
            "as_sval",
            (|args: TokenStream, expr: TokenStream| {
                #[cfg(feature = "sval")]
                {
                    capture_as(
                        "as_sval",
                        args,
                        expr,
                        quote!(__private_capture_as_sval),
                        quote!(__private_capture_anon_as_sval),
                    )
                }
                #[cfg(not(feature = "sval"))]
                {
                    use syn::spanned::Spanned;

                    let _ = args;

                    Err(syn::Error::new(expr.span(), "capturing with `sval` is only possible when the `sval` Cargo feature is enabled"))
                }
            }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>
        );

    map.insert(
            "as_serde",
            (|args: TokenStream, expr: TokenStream| {
                #[cfg(feature = "serde")]
                {
                    capture_as(
                        "as_serde",
                        args,
                        expr,
                        quote!(__private_capture_as_serde),
                        quote!(__private_capture_anon_as_serde),
                    )
                }
                #[cfg(not(feature = "serde"))]
                {
                    use syn::spanned::Spanned;

                    let _ = args;

                    Err(syn::Error::new(expr.span(), "capturing with `serde` is only possible when the `serde` Cargo feature is enabled"))
                }
            }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>
        );

    map.insert(
        "as_error",
        (|args: TokenStream, expr: TokenStream| {
            #[cfg(feature = "std")]
            {
                capture_as(
                    "as_error",
                    args,
                    expr,
                    quote!(__private_capture_as_error),
                    quote!(__private_capture_as_error),
                )
            }
            #[cfg(not(feature = "std"))]
            {
                use syn::spanned::Spanned;

                let _ = args;

                Err(syn::Error::new(
                    expr.span(),
                    "capturing errors is only possible when the `std` Cargo feature is enabled",
                ))
            }
        }) as fn(TokenStream, TokenStream) -> syn::Result<TokenStream>,
    );

    map
}

/**
Format a template.

# Syntax

See the [`macro@emit`] macro for syntax.

# Control parameters

This macro doesn't accept any control parameters.

# Returns

A `String`.
*/
#[proc_macro]
pub fn format(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    format::expand_tokens(format::ExpandTokens {
        input: TokenStream::from(item),
    })
    .unwrap_or_compile_error()
}

/**
Construct an event.

# Syntax

```text
(control_param),* tpl, (property),*
```

where

- `control_param`: A Rust field-value with a pre-determined identifier (see below).
- `tpl`: A template string literal.
- `property`: A Rust field-value for a property to capture.

# Control parameters

This macro accepts the following optional control parameters:

- `module: impl Into<emit::Path>`: The module the event belongs to. If unspecified the current module path is used.
- `props: impl emit::Props`: A base set of properties to add to the event.
- `extent: impl emit::ToExtent`: The extent to use on the event.

# Template

The template for the event. See the [`macro@tpl`] macro for syntax.

# Properties

Properties 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.

# Returns

An `emit::Event`.
*/
#[proc_macro]
pub fn event(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    event::expand_tokens(event::ExpandTokens {
        level: None,
        input: item.into(),
    })
    .unwrap_or_compile_error()
}

/**
Construct a debug event.

# Syntax

See the [`macro@event`] macro for syntax.

# Returns

An `emit::Event`.
*/
#[proc_macro]
pub fn debug_event(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    event::expand_tokens(event::ExpandTokens {
        level: Some(quote!(Debug)),
        input: item.into(),
    })
    .unwrap_or_compile_error()
}

/**
Construct an info event.

# Syntax

See the [`macro@event`] macro for syntax.

# Returns

An `emit::Event`.
*/
#[proc_macro]
pub fn info_event(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    event::expand_tokens(event::ExpandTokens {
        level: Some(quote!(Info)),
        input: item.into(),
    })
    .unwrap_or_compile_error()
}

/**
Construct a warn event.

# Syntax

See the [`macro@event`] macro for syntax.

# Returns

An `emit::Event`.
*/
#[proc_macro]
pub fn warn_event(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    event::expand_tokens(event::ExpandTokens {
        level: Some(quote!(Warn)),
        input: item.into(),
    })
    .unwrap_or_compile_error()
}

/**
Construct an error event.

# Syntax

See the [`macro@event`] macro for syntax.

# Returns

An `emit::Event`.
*/
#[proc_macro]
pub fn error_event(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    event::expand_tokens(event::ExpandTokens {
        level: Some(quote!(Error)),
        input: item.into(),
    })
    .unwrap_or_compile_error()
}

/**
Wrap an operation in a span.

# Syntax

```text
(control_param),* tpl, (property),*
```

where

- `control_param`: A Rust field-value with a pre-determined identifier (see below).
- `tpl`: A template string literal.
- `property`: A Rust field-value for a property to capture.

# Control parameters

This macro accepts the following optional control parameters:

- `rt: impl emit::runtime::Runtime`: The runtime to emit the event through.
- `module: impl Into<emit::Path>`: The module the event belongs to. If unspecified the current module path is used.
- `when: impl emit::Filter`: A filter to use instead of the one configured on the runtime.
- `arg`: An identifier to bind an `emit::Span` to in the body of the span for manual completion.

# Template

The template for the event. See the [`macro@tpl`] macro for syntax.

# Properties

Properties 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.
*/
#[proc_macro_attribute]
pub fn span(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    base_span(None, TokenStream::from(args), TokenStream::from(item))
}

/**
Wrap an operation in a debug span.

# Syntax

See the [`macro@span`] macro for syntax.
*/
#[proc_macro_attribute]
pub fn debug_span(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    base_span(
        Some(quote!(Debug)),
        TokenStream::from(args),
        TokenStream::from(item),
    )
}

/**
Wrap an operation in an info span.

# Syntax

See the [`macro@span`] macro for syntax.
*/
#[proc_macro_attribute]
pub fn info_span(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    base_span(
        Some(quote!(Info)),
        TokenStream::from(args),
        TokenStream::from(item),
    )
}

/**
Wrap an operation in a warn span.

# Syntax

See the [`macro@span`] macro for syntax.
*/
#[proc_macro_attribute]
pub fn warn_span(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    base_span(
        Some(quote!(Warn)),
        TokenStream::from(args),
        TokenStream::from(item),
    )
}

/**
Wrap an operation in an error span.

# Syntax

See the [`macro@span`] macro for syntax.
*/
#[proc_macro_attribute]
pub fn error_span(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    base_span(
        Some(quote!(Error)),
        TokenStream::from(args),
        TokenStream::from(item),
    )
}

/**
Construct a template.

Templates are text literals that include regular text with _holes_. A hole is a point in the template where a property should be interpolated in.

# Syntax

```text
template_literal
```

where

- `template_literal`: `(text | hole)*`
- `text`: A fragment of plain text where `{` are escaped as `{{` and `}` are escaped as `}}`.
- `hole`: `{property}`
- `property`: A Rust field-value of a property to capture.

# Returns

An `emit::Template`.
*/
#[proc_macro]
pub fn tpl(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    build::expand_template_tokens(build::ExpandTemplateTokens {
        input: TokenStream::from(item),
    })
    .unwrap_or_compile_error()
}

/**
Get the parts of a template.

# Syntax

See the [`macro@tpl`] macro for syntax.

# Returns

An `[emit::template::Part; N]` array.
*/
#[proc_macro]
pub fn tpl_parts(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    build::expand_template_parts_tokens(build::ExpandTemplateTokens {
        input: TokenStream::from(item),
    })
    .unwrap_or_compile_error()
}

/**
Get the current timestamp.

# Syntax

```text
(control_param),*
```

where

- `control_param`: A Rust field-value with a pre-determined identifier (see below).

# Control parameters

This macro accepts the following optional control parameters:

- `rt: impl emit::runtime::Runtime`: The runtime to use to read the timestamp from.

# Returns

An `emit::Timestamp`.
*/
#[proc_macro]
pub fn now(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    time::expand_now_tokens(time::ExpandTokens {
        input: TokenStream::from(item),
    })
    .unwrap_or_compile_error()
}

/**
Start a timer.

# Syntax

```text
(control_param),*
```

where

- `control_param`: A Rust field-value with a pre-determined identifier (see below).

# Control parameters

This macro accepts the following optional control parameters:

- `rt: impl emit::runtime::Runtime`: The runtime to use to read the timestamp from.

# Returns

An `emit::Timestamp`.
*/
#[proc_macro]
pub fn start_timer(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    time::expand_start_timer_tokens(time::ExpandTokens {
        input: TokenStream::from(item),
    })
    .unwrap_or_compile_error()
}

/**
Emit an event.

# Syntax

```text
(control_param),* tpl, (property),*
```

where

- `control_param`: A Rust field-value with a pre-determined identifier (see below).
- `tpl`: A template string literal.
- `property`: A Rust field-value for a property to capture.

# Control parameters

This macro accepts the following optional control parameters:

- `rt: impl emit::runtime::Runtime`: The runtime to emit the event through.
- `event: 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 `module`, `props`, and `extent` cannot also be set.
- `module: impl Into<emit::Path>`: The module the event belongs to. If unspecified the current module path is used.
- `props: impl emit::Props`: A base set of properties to add to the event.
- `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.
- `when: impl emit::Filter`: A filter to use instead of the one configured on the runtime.

# Template

The template for the event. See the [`macro@tpl`] macro for syntax.

# Properties

Properties 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.
*/
#[proc_macro]
pub fn emit(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    base_emit(None, TokenStream::from(item))
}

/**
Emit a debug event.

# Syntax

See the [`macro@emit`] macro for syntax.
*/
#[proc_macro]
pub fn debug(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    base_emit(Some(quote!(Debug)), TokenStream::from(item))
}

/**
Emit a info event.

# Syntax

See the [`macro@emit`] macro for syntax.
*/
#[proc_macro]
pub fn info(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    base_emit(Some(quote!(Info)), TokenStream::from(item))
}

/**
Emit a warn event.

# Syntax

See the [`macro@emit`] macro for syntax.
*/
#[proc_macro]
pub fn warn(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    base_emit(Some(quote!(Warn)), TokenStream::from(item))
}

/**
Emit an error event.

# Syntax

See the [`macro@emit`] macro for syntax.
*/
#[proc_macro]
pub fn error(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    base_emit(Some(quote!(Error)), TokenStream::from(item))
}

/**
Construct a set of properties.

# Syntax

```text
(property),*
```

where

- `property`: A Rust field-value for a property. The identifier of the field-value is the key of the property.
*/
#[proc_macro]
pub fn props(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
    build::expand_props_tokens(build::ExpandPropsTokens {
        input: TokenStream::from(item),
    })
    .unwrap_or_compile_error()
}

/**
Specify Rust format flags to use when rendering a property in a template.

# Syntax

```text
fmt_string
```

where

- `fmt_string`: A string literal with the format flags, like `":?"`. See the [`std::fmt`] docs for details on available flags.

# Applicable to

This attribute can be applied to properties that appear in a template.
*/
#[proc_macro_attribute]
pub fn fmt(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    (hook::get("fmt").unwrap())(TokenStream::from(args), TokenStream::from(item))
        .unwrap_or_compile_error()
}

/**
Specify the key for a property.

# Syntax

```text
key
```

where

- `key`: A string literal with the key to use. The key doesn't need to be a valid Rust identifier.

# Applicable to

This attribute can be applied to properties.
*/
#[proc_macro_attribute]
pub fn key(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    (hook::get("key").unwrap())(TokenStream::from(args), TokenStream::from(item))
        .unwrap_or_compile_error()
}

/**
Specify that a property value of `None` should not be captured, instead of being captured as `null`.

# Syntax

This macro doesn't accept any arguments.

# Applicable to

This attribute can be applied to properties.
*/
#[proc_macro_attribute]
pub fn optional(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    (hook::get("optional").unwrap())(TokenStream::from(args), TokenStream::from(item))
        .unwrap_or_compile_error()
}

/**
Capture a property using its `ToValue` implementation.

# Syntax

This macro doesn't accept any arguments.

# Applicable to

This attribute can be applied to properties.
*/
#[proc_macro_attribute]
pub fn as_value(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    (hook::get("as_value").unwrap())(TokenStream::from(args), TokenStream::from(item))
        .unwrap_or_compile_error()
}

/**
Capture a property using its `Debug` implementation.

# Syntax

This macro doesn't accept any arguments.

# Applicable to

This attribute can be applied to properties.
*/
#[proc_macro_attribute]
pub fn as_debug(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    (hook::get("as_debug").unwrap())(TokenStream::from(args), TokenStream::from(item))
        .unwrap_or_compile_error()
}

/**
Capture a property using its `Display` implementation.

# Syntax

This macro doesn't accept any arguments.

# Applicable to

This attribute can be applied to properties.
*/
#[proc_macro_attribute]
pub fn as_display(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    (hook::get("as_display").unwrap())(TokenStream::from(args), TokenStream::from(item))
        .unwrap_or_compile_error()
}

/**
Capture a property using its `sval::Value` implementation.

# Syntax

This macro doesn't accept any arguments.

# Applicable to

This attribute can be applied to properties.
*/
#[proc_macro_attribute]
pub fn as_sval(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    (hook::get("as_sval").unwrap())(TokenStream::from(args), TokenStream::from(item))
        .unwrap_or_compile_error()
}

/**
Capture a property using its `serde::Serialize` implementation.

# Syntax

This macro doesn't accept any arguments.

# Applicable to

This attribute can be applied to properties.
*/
#[proc_macro_attribute]
pub fn as_serde(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    (hook::get("as_serde").unwrap())(TokenStream::from(args), TokenStream::from(item))
        .unwrap_or_compile_error()
}

/**
Capture a property using its `Error` implementation.

# Syntax

This macro doesn't accept any arguments.

# Applicable to

This attribute can be applied to properties.
*/
#[proc_macro_attribute]
pub fn as_error(
    args: proc_macro::TokenStream,
    item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
    (hook::get("as_error").unwrap())(TokenStream::from(args), TokenStream::from(item))
        .unwrap_or_compile_error()
}

fn base_emit(level: Option<TokenStream>, item: TokenStream) -> proc_macro::TokenStream {
    if filter::matches_build_filter() {
        emit::expand_tokens(emit::ExpandTokens { level, input: item }).unwrap_or_compile_error()
    } else {
        proc_macro::TokenStream::new()
    }
}

fn base_span(
    level: Option<TokenStream>,
    input: TokenStream,
    item: TokenStream,
) -> proc_macro::TokenStream {
    if filter::matches_build_filter() {
        span::expand_tokens(span::ExpandTokens { level, input, item }).unwrap_or_compile_error()
    } else {
        item.into()
    }
}

fn capture_as(
    name: &'static str,
    args: TokenStream,
    expr: TokenStream,
    as_fn: TokenStream,
    as_anon_fn: TokenStream,
) -> syn::Result<TokenStream> {
    capture::rename_hook_tokens(capture::RenameHookTokens {
        name,
        args,
        expr,
        to: |args: &capture::Args| {
            if args.inspect {
                as_fn.clone()
            } else {
                as_anon_fn.clone()
            }
        },
    })
}