borrowscope-macro 0.1.1

Procedural macros for BorrowScope ownership tracking
Documentation
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
//! Configuration for the `#[trace_borrow]` attribute.
//!
//! This module provides [`TraceConfig`] for controlling which operations are tracked
//! and [`TraceArgs`] for parsing attribute arguments.
//!
//! # Feature Groups
//!
//! | Group | Config Field | Description |
//! |-------|--------------|-------------|
//! | `ownership` | `track_new`, `track_move`, `track_drop`, `track_borrow` | Basic ownership |
//! | `smart_pointers` | `track_smart_pointers` | Rc, Arc, RefCell, Cell |
//! | `loops` | `track_loops` | for, while, loop |
//! | `branches` | `track_branches` | if/else, match |
//! | `control_flow` | `track_control_flow` | break, continue, return |
//! | `try` | `track_try` | ? operator |
//! | `methods` | `track_methods` | clone, lock, unwrap |
//! | `async` | `track_async` | async blocks, await |
//! | `unsafe` | `track_unsafe` | unsafe blocks, pointers |
//! | `expressions` | `track_expressions` | struct, tuple, array, range, cast |
//! | `functions` | `track_functions` | Function entry/exit |
//!
//! # Conditional Compilation
//!
//! | Option | Description |
//! |--------|-------------|
//! | `debug_only` | Only instrument in debug builds (`#[cfg(debug_assertions)]`) |
//! | `release_only` | Only instrument in release builds (`#[cfg(not(debug_assertions))]`) |
//! | `feature = "name"` | Only instrument when cargo feature is enabled |
//!
//! # Usage
//!
//! ```ignore
//! #[trace_borrow]                           // standard (default)
//! #[trace_borrow(quiet)]                    // ownership only
//! #[trace_borrow(verbose)]                  // all features
//! #[trace_borrow(skip = "loops,branches")]  // skip specific groups
//! #[trace_borrow(only = "ownership")]       // only specific groups
//! #[trace_borrow(debug_only)]               // only in debug builds
//! #[trace_borrow(feature = "tracing")]      // only when feature enabled
//! ```

use syn::{
    parse::{Parse, ParseStream},
    punctuated::Punctuated,
    Ident, LitStr, Token,
};

/// Conditional compilation mode for tracking code.
#[derive(Debug, Clone, Default, PartialEq)]
pub enum ConditionalMode {
    /// Always generate tracking code (default)
    #[default]
    Always,
    /// Only generate tracking code in debug builds
    DebugOnly,
    /// Only generate tracking code in release builds
    ReleaseOnly,
    /// Only generate tracking code when a specific feature is enabled
    Feature(String),
}

impl ConditionalMode {
    /// Generate the cfg attribute tokens for this mode.
    /// Returns None for Always mode (no cfg needed).
    pub fn cfg_tokens(&self) -> Option<proc_macro2::TokenStream> {
        use quote::quote;
        match self {
            ConditionalMode::Always => None,
            ConditionalMode::DebugOnly => Some(quote! { #[cfg(debug_assertions)] }),
            ConditionalMode::ReleaseOnly => Some(quote! { #[cfg(not(debug_assertions))] }),
            ConditionalMode::Feature(name) => {
                let feature_name = syn::LitStr::new(name, proc_macro2::Span::call_site());
                Some(quote! { #[cfg(feature = #feature_name)] })
            }
        }
    }

    /// Check if this mode requires conditional compilation.
    pub fn is_conditional(&self) -> bool {
        !matches!(self, ConditionalMode::Always)
    }
}

/// Configuration for what operations to track.
///
/// Each field controls a category of tracking. All fields default to `true`
/// in standard mode except `track_functions` which defaults to `false`.
///
/// # Example
///
/// ```ignore
/// let config = TraceConfig::standard();
/// assert!(config.track_new);
/// assert!(config.track_borrow);
/// assert!(!config.track_functions); // disabled by default
/// ```
#[derive(Debug, Clone, Default)]
pub struct TraceConfig {
    /// Track variable creation via `let x = value;`
    ///
    /// Generates `New` events with variable name and type.
    pub track_new: bool,

    /// Track ownership moves via `let y = x;`
    ///
    /// Generates `Move` events showing source and destination.
    pub track_move: bool,

    /// Track variable drops at scope exit.
    ///
    /// Generates `Drop` events in LIFO order.
    pub track_drop: bool,

    /// Track borrows via `&x` and `&mut x`.
    ///
    /// Generates `Borrow` events with mutability info.
    pub track_borrow: bool,

    /// Track smart pointer operations (Rc, Arc, RefCell, Cell).
    ///
    /// Generates events like `RcNew`, `RcClone`, `RefCellBorrow`, etc.
    pub track_smart_pointers: bool,

    /// Track loop constructs (for, while, loop).
    ///
    /// Generates `LoopEnter`, `LoopIteration`, `LoopExit` events.
    pub track_loops: bool,

    /// Track branching (if/else, match).
    ///
    /// Generates `Branch`, `MatchEnter`, `MatchArm`, `MatchExit` events.
    pub track_branches: bool,

    /// Track control flow (break, continue, return).
    ///
    /// Generates `Break`, `Continue`, `Return` events.
    pub track_control_flow: bool,

    /// Track the `?` operator.
    ///
    /// Generates `Try` events at each `?` usage.
    pub track_try: bool,

    /// Track method calls (clone, lock, unwrap).
    ///
    /// Generates `Clone`, `Lock`, `Unwrap` events.
    pub track_methods: bool,

    /// Track async operations (async blocks, await).
    ///
    /// Generates `AsyncBlockEnter/Exit`, `AwaitStart/End` events.
    pub track_async: bool,

    /// Track unsafe blocks and operations.
    ///
    /// Generates `UnsafeBlockEnter/Exit`, `RawPtrCreated`, `Transmute` events.
    pub track_unsafe: bool,

    /// Track expression constructs (struct, tuple, array, range, cast).
    ///
    /// Generates `StructCreate`, `TupleCreate`, `ArrayCreate`, `Range`, `TypeCast` events.
    pub track_expressions: bool,

    /// Track function entry and exit points.
    ///
    /// Generates `FnEnter` and `FnExit` events. Disabled by default.
    pub track_functions: bool,

    /// Conditional compilation mode.
    ///
    /// Controls when tracking code is compiled:
    /// - `Always` (default): Always compile tracking code
    /// - `DebugOnly`: Only compile in debug builds
    /// - `ReleaseOnly`: Only compile in release builds
    /// - `Feature(name)`: Only compile when cargo feature is enabled
    pub conditional_mode: ConditionalMode,

    /// Emit warnings for ambiguous patterns.
    ///
    /// When enabled, the macro will emit compile-time warnings for patterns
    /// that cannot be fully analyzed due to the type information barrier.
    pub warn_ambiguous: bool,

    /// Known FFI function names (won't warn about these).
    pub known_ffi: Vec<String>,

    /// Known union type names (won't warn about these).
    pub known_unions: Vec<String>,

    /// Known static variable names (won't warn about these).
    pub known_statics: Vec<String>,

    /// Filter pattern for variable names.
    ///
    /// Only track variables matching this pattern. Supports:
    /// - `name:prefix*` - Variables starting with prefix
    /// - `name:*suffix` - Variables ending with suffix
    /// - `name:*contains*` - Variables containing substring
    /// - `name:exact` - Exact match
    pub filter_pattern: Option<String>,

    /// Sample rate for tracking (0.0 to 1.0).
    ///
    /// When set, only a random percentage of tracking calls are executed.
    /// - `1.0` (default): Track all calls
    /// - `0.5`: Track ~50% of calls
    /// - `0.01`: Track ~1% of calls
    pub sample_rate: Option<f64>,
}

impl TraceConfig {
    /// Create standard configuration with all tracking enabled except functions.
    ///
    /// This is the default when using `#[trace_borrow]` without arguments.
    pub fn standard() -> Self {
        Self {
            track_new: true,
            track_move: true,
            track_drop: true,
            track_borrow: true,
            track_smart_pointers: true,
            track_loops: true,
            track_branches: true,
            track_control_flow: true,
            track_try: true,
            track_methods: true,
            track_async: true,
            track_unsafe: true,
            track_expressions: true,
            track_functions: false,
            conditional_mode: ConditionalMode::Always,
            warn_ambiguous: false,
            known_ffi: Vec::new(),
            known_unions: Vec::new(),
            known_statics: Vec::new(),
            filter_pattern: None,
            sample_rate: None,
        }
    }

    /// Create quiet configuration with only ownership tracking.
    ///
    /// Used with `#[trace_borrow(quiet)]`. Tracks only:
    /// - Variable creation (new)
    /// - Moves
    /// - Drops
    /// - Borrows
    pub fn quiet() -> Self {
        Self {
            track_new: true,
            track_move: true,
            track_drop: true,
            track_borrow: true,
            track_smart_pointers: false,
            track_loops: false,
            track_branches: false,
            track_control_flow: false,
            track_try: false,
            track_methods: false,
            track_async: false,
            track_unsafe: false,
            track_expressions: false,
            track_functions: false,
            conditional_mode: ConditionalMode::Always,
            warn_ambiguous: false,
            known_ffi: Vec::new(),
            known_unions: Vec::new(),
            known_statics: Vec::new(),
            filter_pattern: None,
            sample_rate: None,
        }
    }

    /// Create verbose configuration with all tracking enabled.
    ///
    /// Used with `#[trace_borrow(verbose)]`. Same as standard for now.
    pub fn verbose() -> Self {
        Self::standard()
    }

    /// Disable specific feature groups.
    ///
    /// Used with `#[trace_borrow(skip = "loops,branches")]`.
    ///
    /// # Supported Groups
    ///
    /// - `loops` - Disable loop tracking
    /// - `branches` - Disable if/else and match tracking
    /// - `control_flow` or `control` - Disable break/continue/return
    /// - `try` - Disable ? operator tracking
    /// - `methods` - Disable clone/lock/unwrap tracking
    /// - `async` - Disable async block and await tracking
    /// - `unsafe` - Disable unsafe block tracking
    /// - `expressions` or `exprs` - Disable struct/tuple/array/range/cast
    /// - `smart_pointers` or `pointers` - Disable Rc/Arc/RefCell/Cell
    /// - `functions` or `fn` - Disable function entry/exit
    pub fn skip(&mut self, features: &str) {
        for feature in features.split(',').map(|s| s.trim()) {
            match feature {
                "loops" => self.track_loops = false,
                "branches" => self.track_branches = false,
                "control_flow" | "control" => self.track_control_flow = false,
                "try" => self.track_try = false,
                "methods" => self.track_methods = false,
                "async" => self.track_async = false,
                "unsafe" => self.track_unsafe = false,
                "expressions" | "exprs" => self.track_expressions = false,
                "smart_pointers" | "pointers" => self.track_smart_pointers = false,
                "functions" | "fn" => self.track_functions = false,
                "drop" | "drops" => self.track_drop = false,
                _ => {} // ignore unknown
            }
        }
    }

    /// Enable only specific feature groups, disabling all others.
    ///
    /// Used with `#[trace_borrow(only = "ownership")]`.
    ///
    /// # Supported Groups
    ///
    /// - `ownership` - Enable new, move, drop, borrow
    /// - `new` - Enable only variable creation
    /// - `move` or `moves` - Enable only move tracking
    /// - `drop` or `drops` - Enable only drop tracking
    /// - `borrow` or `borrows` - Enable only borrow tracking
    /// - `loops` - Enable loop tracking
    /// - `branches` - Enable if/else and match tracking
    /// - `control_flow` or `control` - Enable break/continue/return
    /// - `try` - Enable ? operator tracking
    /// - `methods` - Enable clone/lock/unwrap tracking
    /// - `async` - Enable async block and await tracking
    /// - `unsafe` - Enable unsafe block tracking
    /// - `expressions` or `exprs` - Enable struct/tuple/array/range/cast
    /// - `smart_pointers` or `pointers` - Enable Rc/Arc/RefCell/Cell
    /// - `functions` or `fn` - Enable function entry/exit
    pub fn only(&mut self, features: &str) {
        // Preserve settings that shouldn't be reset
        let mode = self.conditional_mode.clone();
        let warn = self.warn_ambiguous;
        let ffi = std::mem::take(&mut self.known_ffi);
        let unions = std::mem::take(&mut self.known_unions);
        let statics = std::mem::take(&mut self.known_statics);
        let filter = self.filter_pattern.take();
        let sample = self.sample_rate.take();

        // Start with nothing
        *self = Self {
            track_new: false,
            track_move: false,
            track_drop: false,
            track_borrow: false,
            track_smart_pointers: false,
            track_loops: false,
            track_branches: false,
            track_control_flow: false,
            track_try: false,
            track_methods: false,
            track_async: false,
            track_unsafe: false,
            track_expressions: false,
            track_functions: false,
            conditional_mode: mode,
            warn_ambiguous: warn,
            known_ffi: ffi,
            known_unions: unions,
            known_statics: statics,
            filter_pattern: filter,
            sample_rate: sample,
        };

        for feature in features.split(',').map(|s| s.trim()) {
            match feature {
                "ownership" => {
                    self.track_new = true;
                    self.track_move = true;
                    self.track_drop = true;
                    self.track_borrow = true;
                }
                "new" => self.track_new = true,
                "move" | "moves" => self.track_move = true,
                "drop" | "drops" => self.track_drop = true,
                "borrow" | "borrows" => self.track_borrow = true,
                "loops" => self.track_loops = true,
                "branches" => self.track_branches = true,
                "control_flow" | "control" => self.track_control_flow = true,
                "try" => self.track_try = true,
                "methods" => self.track_methods = true,
                "async" => self.track_async = true,
                "unsafe" => self.track_unsafe = true,
                "expressions" | "exprs" => self.track_expressions = true,
                "smart_pointers" | "pointers" => self.track_smart_pointers = true,
                "functions" | "fn" => self.track_functions = true,
                _ => {} // ignore unknown
            }
        }
    }
}

/// Parsed attribute arguments for `#[trace_borrow(...)]`.
///
/// This struct is used internally to parse the attribute arguments.
pub struct TraceArgs {
    /// The resulting configuration after parsing arguments.
    pub config: TraceConfig,
}

impl Default for TraceArgs {
    fn default() -> Self {
        Self {
            config: TraceConfig::standard(),
        }
    }
}

impl Parse for TraceArgs {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        if input.is_empty() {
            return Ok(Self::default());
        }

        let mut config = TraceConfig::standard();

        // Parse comma-separated arguments
        let args: Punctuated<TraceArg, Token![,]> = Punctuated::parse_terminated(input)?;

        for arg in args {
            match arg {
                TraceArg::Verbose => config = TraceConfig::verbose(),
                TraceArg::Quiet => config = TraceConfig::quiet(),
                TraceArg::Skip(features) => config.skip(&features),
                TraceArg::Only(features) => config.only(&features),
                TraceArg::DebugOnly => config.conditional_mode = ConditionalMode::DebugOnly,
                TraceArg::ReleaseOnly => config.conditional_mode = ConditionalMode::ReleaseOnly,
                TraceArg::Feature(name) => config.conditional_mode = ConditionalMode::Feature(name),
                TraceArg::WarnAmbiguous => config.warn_ambiguous = true,
                TraceArg::Ffi(names) => config.known_ffi.extend(names),
                TraceArg::Unions(names) => config.known_unions.extend(names),
                TraceArg::Statics(names) => config.known_statics.extend(names),
                TraceArg::Filter(pattern) => config.filter_pattern = Some(pattern),
                TraceArg::Sample(rate) => config.sample_rate = Some(rate),
            }
        }

        Ok(Self { config })
    }
}

#[derive(Debug)]
enum TraceArg {
    Verbose,
    Quiet,
    Skip(String),
    Only(String),
    DebugOnly,
    ReleaseOnly,
    Feature(String),
    WarnAmbiguous,
    Ffi(Vec<String>),
    Unions(Vec<String>),
    Statics(Vec<String>),
    Filter(String),
    Sample(f64),
}

impl Parse for TraceArg {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let ident: Ident = input.parse()?;
        let name = ident.to_string();

        match name.as_str() {
            "verbose" => Ok(TraceArg::Verbose),
            "quiet" => Ok(TraceArg::Quiet),
            "debug_only" => Ok(TraceArg::DebugOnly),
            "release_only" => Ok(TraceArg::ReleaseOnly),
            "skip" => {
                // Support both skip = "..." and skip(...)
                if input.peek(Token![=]) {
                    input.parse::<Token![=]>()?;
                    let value: LitStr = input.parse()?;
                    Ok(TraceArg::Skip(value.value()))
                } else {
                    let content;
                    syn::parenthesized!(content in input);
                    let items: Punctuated<Ident, Token![,]> = Punctuated::parse_terminated(&content)?;
                    let value = items.iter().map(|i| i.to_string()).collect::<Vec<_>>().join(", ");
                    Ok(TraceArg::Skip(value))
                }
            }
            "only" => {
                // Support both only = "..." and only(...)
                if input.peek(Token![=]) {
                    input.parse::<Token![=]>()?;
                    let value: LitStr = input.parse()?;
                    Ok(TraceArg::Only(value.value()))
                } else {
                    let content;
                    syn::parenthesized!(content in input);
                    let items: Punctuated<Ident, Token![,]> = Punctuated::parse_terminated(&content)?;
                    let value = items.iter().map(|i| i.to_string()).collect::<Vec<_>>().join(", ");
                    Ok(TraceArg::Only(value))
                }
            }
            "feature" => {
                input.parse::<Token![=]>()?;
                let value: LitStr = input.parse()?;
                Ok(TraceArg::Feature(value.value()))
            }
            "warn" | "warn_ambiguous" => Ok(TraceArg::WarnAmbiguous),
            "ffi" => {
                input.parse::<Token![=]>()?;
                let names = parse_string_list(input)?;
                Ok(TraceArg::Ffi(names))
            }
            "unions" => {
                input.parse::<Token![=]>()?;
                let names = parse_string_list(input)?;
                Ok(TraceArg::Unions(names))
            }
            "statics" => {
                input.parse::<Token![=]>()?;
                let names = parse_string_list(input)?;
                Ok(TraceArg::Statics(names))
            }
            "filter" => {
                input.parse::<Token![=]>()?;
                let value: LitStr = input.parse()?;
                let pattern = value.value();
                
                // Validate filter pattern
                if pattern.is_empty() {
                    return Err(syn::Error::new(
                        value.span(),
                        "filter pattern cannot be empty"
                    ));
                }
                
                // Check for invalid characters (only alphanumeric, _, *, ? allowed)
                for ch in pattern.chars() {
                    if !ch.is_alphanumeric() && ch != '_' && ch != '*' && ch != '?' {
                        return Err(syn::Error::new(
                            value.span(),
                            format!(
                                "invalid character '{}' in filter pattern. \
                                 Only alphanumeric, '_', '*', and '?' are allowed",
                                ch
                            )
                        ));
                    }
                }
                
                Ok(TraceArg::Filter(pattern))
            }
            "sample" => {
                input.parse::<Token![=]>()?;
                let value: syn::LitFloat = input.parse()?;
                let rate: f64 = value.base10_parse()?;
                
                // Validate sample rate range
                if rate < 0.0 || rate > 1.0 {
                    return Err(syn::Error::new(
                        value.span(),
                        format!(
                            "sample rate must be between 0.0 and 1.0, got {}",
                            rate
                        )
                    ));
                }
                
                Ok(TraceArg::Sample(rate))
            }
            _ => Err(syn::Error::new(
                ident.span(),
                format!(
                    "unknown attribute argument: {}. Expected one of: verbose, quiet, \
                     debug_only, release_only, skip, only, feature, warn, ffi, unions, statics, filter, sample",
                    name
                ),
            )),
        }
    }
}

/// Parse a list of strings in bracket syntax: ["a", "b", "c"]
fn parse_string_list(input: ParseStream) -> syn::Result<Vec<String>> {
    let content;
    syn::bracketed!(content in input);
    let items: Punctuated<LitStr, Token![,]> = Punctuated::parse_terminated(&content)?;
    Ok(items.iter().map(|s| s.value()).collect())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_standard_config() {
        let config = TraceConfig::standard();
        assert!(config.track_new);
        assert!(config.track_loops);
        assert!(config.track_branches);
        assert_eq!(config.conditional_mode, ConditionalMode::Always);
    }

    #[test]
    fn test_quiet_config() {
        let config = TraceConfig::quiet();
        assert!(config.track_new);
        assert!(!config.track_loops);
        assert!(!config.track_branches);
        assert_eq!(config.conditional_mode, ConditionalMode::Always);
    }

    #[test]
    fn test_skip() {
        let mut config = TraceConfig::standard();
        config.skip("loops, branches");
        assert!(!config.track_loops);
        assert!(!config.track_branches);
        assert!(config.track_new);
    }

    #[test]
    fn test_only_ownership() {
        let mut config = TraceConfig::standard();
        config.only("ownership");
        assert!(config.track_new);
        assert!(config.track_move);
        assert!(config.track_drop);
        assert!(config.track_borrow);
        assert!(!config.track_loops);
        assert!(!config.track_branches);
    }

    #[test]
    fn test_conditional_mode_debug_only() {
        let mut config = TraceConfig::standard();
        config.conditional_mode = ConditionalMode::DebugOnly;
        assert!(config.conditional_mode.is_conditional());
        assert!(config.conditional_mode.cfg_tokens().is_some());
    }

    #[test]
    fn test_conditional_mode_release_only() {
        let mut config = TraceConfig::standard();
        config.conditional_mode = ConditionalMode::ReleaseOnly;
        assert!(config.conditional_mode.is_conditional());
        assert!(config.conditional_mode.cfg_tokens().is_some());
    }

    #[test]
    fn test_conditional_mode_feature() {
        let mut config = TraceConfig::standard();
        config.conditional_mode = ConditionalMode::Feature("tracing".to_string());
        assert!(config.conditional_mode.is_conditional());
        assert!(config.conditional_mode.cfg_tokens().is_some());
    }

    #[test]
    fn test_conditional_mode_always() {
        let config = TraceConfig::standard();
        assert!(!config.conditional_mode.is_conditional());
        assert!(config.conditional_mode.cfg_tokens().is_none());
    }

    #[test]
    fn test_only_preserves_conditional_mode() {
        let mut config = TraceConfig::standard();
        config.conditional_mode = ConditionalMode::DebugOnly;
        config.only("ownership");
        assert_eq!(config.conditional_mode, ConditionalMode::DebugOnly);
    }

    #[test]
    fn test_filter_pattern() {
        let mut config = TraceConfig::standard();
        config.filter_pattern = Some("data*".to_string());
        assert_eq!(config.filter_pattern, Some("data*".to_string()));
    }

    #[test]
    fn test_sample_rate() {
        let mut config = TraceConfig::standard();
        config.sample_rate = Some(0.1);
        assert_eq!(config.sample_rate, Some(0.1));
    }

    #[test]
    fn test_only_preserves_filter_and_sample() {
        let mut config = TraceConfig::standard();
        config.filter_pattern = Some("user*".to_string());
        config.sample_rate = Some(0.5);
        config.only("ownership");
        assert_eq!(config.filter_pattern, Some("user*".to_string()));
        assert_eq!(config.sample_rate, Some(0.5));
    }

    #[test]
    fn test_filter_pattern_validation_valid() {
        // Valid patterns should parse
        let valid = ["data*", "*_count", "user_?", "abc123", "a*b?c"];
        for pattern in valid {
            let input = format!("filter = \"{}\"", pattern);
            let result: syn::Result<TraceArg> = syn::parse_str(&input);
            assert!(result.is_ok(), "Pattern '{}' should be valid", pattern);
        }
    }

    #[test]
    fn test_filter_pattern_validation_empty() {
        let result: syn::Result<TraceArg> = syn::parse_str("filter = \"\"");
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("cannot be empty"));
    }

    #[test]
    fn test_filter_pattern_validation_invalid_chars() {
        let invalid = ["data-name", "user.name", "path/to", "a@b", "x#y"];
        for pattern in invalid {
            let input = format!("filter = \"{}\"", pattern);
            let result: syn::Result<TraceArg> = syn::parse_str(&input);
            assert!(result.is_err(), "Pattern '{}' should be invalid", pattern);
            assert!(result.unwrap_err().to_string().contains("invalid character"));
        }
    }

    #[test]
    fn test_sample_rate_validation_valid() {
        // Note: syn requires explicit float syntax (with decimal point)
        let valid = ["0.0", "0.1", "0.5", "1.0"];
        for rate in valid {
            let input = format!("sample = {}", rate);
            let result: syn::Result<TraceArg> = syn::parse_str(&input);
            assert!(result.is_ok(), "Rate {} should be valid", rate);
        }
    }

    #[test]
    fn test_sample_rate_validation_out_of_range() {
        let invalid = ["-0.1", "1.1", "2.0", "-1.0"];
        for rate in invalid {
            let input = format!("sample = {}", rate);
            let result: syn::Result<TraceArg> = syn::parse_str(&input);
            assert!(result.is_err(), "Rate {} should be invalid", rate);
            assert!(result.unwrap_err().to_string().contains("between 0.0 and 1.0"));
        }
    }
}