ferronconf 0.2.0

A Rust library for parsing `ferron.conf` configuration files — a domain-specific language for Ferron web server configurations.
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
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
//! Abstract Syntax Tree (AST) types for `ferron.conf` configuration files.
//!
//! This module defines the complete structure of a parsed configuration file,
//! including all statement types, values, and helper methods for navigation.
//!
//! # Example
//!
//! ```rust
//! use ferronconf::Config;
//! use std::str::FromStr;
//!
//! let input = r#"
//! example.com {
//!     root /var/www/example
//! }
//! "#;
//!
//! let config = Config::from_str(input).unwrap();
//! ```

mod display;

use crate::lexer::Span;
use crate::{Lexer, ParseError, Parser};
use std::fmt::Write;
use std::net::IpAddr;
use std::str::FromStr;

/// The root AST node representing a complete `ferron.conf` configuration file.
///
/// A `Config` contains a sequence of [`Statement`]s at the top level.
/// Use the helper methods to find specific directives or blocks.
///
/// # Example
///
/// ```rust
/// use ferronconf::Config;
/// use std::str::FromStr;
///
/// let config = Config::from_str("example.com { root /var/www }").unwrap();
/// let host_blocks = config.find_host_blocks();
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Config {
    /// The top-level statements in the configuration file.
    pub statements: Vec<Statement>,
}

impl Config {
    /// Finds all [`Directive`] statements with the given name.
    ///
    /// # Arguments
    ///
    /// * `name` - The directive name to search for (e.g., `"root"`, `"server_name"`)
    ///
    /// # Returns
    ///
    /// A vector of references to matching directives.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use ferronconf::Config;
    /// # use std::str::FromStr;
    /// let config = Config::from_str("root /var/www\nroot /var/www/other").unwrap();
    /// let roots = config.find_directives("root");
    /// assert_eq!(roots.len(), 2);
    /// ```
    pub fn find_directives(&self, name: &str) -> Vec<&Directive> {
        self.statements
            .iter()
            .filter_map(|stmt| {
                if let Statement::Directive(directive) = stmt {
                    if directive.name == name {
                        Some(directive)
                    } else {
                        None
                    }
                } else {
                    None
                }
            })
            .collect()
    }

    /// Finds all [`HostBlock`] statements in the configuration.
    ///
    /// # Returns
    ///
    /// A vector of references to all host blocks.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use ferronconf::Config;
    /// # use std::str::FromStr;
    /// let config = Config::from_str("example.com { root /var/www }").unwrap();
    /// let hosts = config.find_host_blocks();
    /// assert_eq!(hosts.len(), 1);
    /// ```
    pub fn find_host_blocks(&self) -> Vec<&HostBlock> {
        self.statements
            .iter()
            .filter_map(|stmt| {
                if let Statement::HostBlock(host_block) = stmt {
                    Some(host_block)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Finds all [`MatchBlock`] statements in the configuration.
    ///
    /// # Returns
    ///
    /// A vector of references to all match blocks.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use ferronconf::Config;
    /// # use std::str::FromStr;
    /// let config = Config::from_str("match rules { path == \"/api\" }").unwrap();
    /// let matchers = config.find_match_blocks();
    /// assert_eq!(matchers.len(), 1);
    /// ```
    pub fn find_match_blocks(&self) -> Vec<&MatchBlock> {
        self.statements
            .iter()
            .filter_map(|stmt| {
                if let Statement::MatchBlock(match_block) = stmt {
                    Some(match_block)
                } else {
                    None
                }
            })
            .collect()
    }
}

impl FromStr for Config {
    type Err = ParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let lexer = Lexer::new(s);
        let mut parser = Parser::new(lexer.collect());
        parser.parse_config()
    }
}

/// A statement in the configuration file.
///
/// Statements are the top-level building blocks of a `ferron.conf` file.
/// There are five types of statements, each serving a different purpose.
#[derive(Debug, Clone, PartialEq)]
pub enum Statement {
    /// A key-value directive with optional arguments and nested block.
    Directive(Directive),
    /// A host-specific configuration block (top-level only).
    HostBlock(HostBlock),
    /// A conditional match block for request attributes.
    MatchBlock(MatchBlock),
    /// A global configuration block (top-level only).
    GlobalBlock(Block),
    /// A reusable configuration snippet definition.
    SnippetBlock(SnippetBlock),
}

impl Statement {
    /// Returns the source span (line and column) of this statement.
    ///
    /// Useful for error reporting and debugging.
    pub fn span(&self) -> Span {
        match self {
            Statement::Directive(d) => d.span,
            Statement::HostBlock(h) => h.span,
            Statement::MatchBlock(m) => m.span,
            Statement::GlobalBlock(g) => g.span,
            Statement::SnippetBlock(s) => s.span,
        }
    }

    /// Returns `true` if this statement is a [`Directive`].
    pub fn is_directive(&self) -> bool {
        matches!(self, Statement::Directive(_))
    }

    /// Returns `true` if this statement is a [`HostBlock`].
    pub fn is_host_block(&self) -> bool {
        matches!(self, Statement::HostBlock(_))
    }

    /// Returns `true` if this statement is a [`MatchBlock`].
    pub fn is_match_block(&self) -> bool {
        matches!(self, Statement::MatchBlock(_))
    }

    /// Returns `true` if this statement is a global block.
    pub fn is_global_block(&self) -> bool {
        matches!(self, Statement::GlobalBlock(_))
    }

    /// Returns `true` if this statement is a [`SnippetBlock`].
    pub fn is_snippet_block(&self) -> bool {
        matches!(self, Statement::SnippetBlock(_))
    }
}

/// A configuration directive with a name, optional arguments, and an optional nested block.
///
/// Directives are the primary way to specify configuration values.
///
/// # Example
///
/// ```ferron
/// server_name example.com
/// max_connections 1000
/// root "{{app.root}}" {
///     index index.html
/// }
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct Directive {
    /// The directive name (e.g., `"root"`, `"server_name"`).
    pub name: String,
    /// The directive arguments (values).
    pub args: Vec<Value>,
    /// An optional nested block of statements.
    pub block: Option<Block>,
    /// The source span of the directive.
    pub span: Span,
}

impl Directive {
    /// Gets the string argument at the given index.
    ///
    /// # Arguments
    ///
    /// * `index` - The zero-based index of the argument
    ///
    /// # Returns
    ///
    /// `Some(&str)` if the argument exists and is a string, `None` otherwise.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use ferronconf::Config;
    /// # use std::str::FromStr;
    /// let config = Config::from_str("server_name example.com").unwrap();
    /// if let Some(d) = config.find_directives("server_name").first() {
    ///     assert_eq!(d.get_string_arg(0), Some("example.com"));
    /// }
    /// ```
    pub fn get_string_arg(&self, index: usize) -> Option<&str> {
        self.args.get(index).and_then(|arg| {
            if let Value::String(s, _) = arg {
                Some(s.as_str())
            } else {
                None
            }
        })
    }

    /// Gets the integer argument at the given index.
    ///
    /// # Arguments
    ///
    /// * `index` - The zero-based index of the argument
    ///
    /// # Returns
    ///
    /// `Some(i64)` if the argument exists and is an integer, `None` otherwise.
    pub fn get_integer_arg(&self, index: usize) -> Option<i64> {
        self.args.get(index).and_then(|arg| {
            if let Value::Integer(i, _) = arg {
                Some(*i)
            } else {
                None
            }
        })
    }

    /// Gets the boolean argument at the given index.
    ///
    /// # Arguments
    ///
    /// * `index` - The zero-based index of the argument
    ///
    /// # Returns
    ///
    /// `Some(bool)` if the argument exists and is a boolean, `None` otherwise.
    pub fn get_boolean_arg(&self, index: usize) -> Option<bool> {
        self.args.get(index).and_then(|arg| {
            if let Value::Boolean(b, _) = arg {
                Some(*b)
            } else {
                None
            }
        })
    }

    /// Returns `true` if this directive has a nested block.
    pub fn has_block(&self) -> bool {
        self.block.is_some()
    }
}

/// A conditional match block that evaluates request attributes.
///
/// Match blocks contain a list of expressions that are evaluated
/// to determine if certain configuration should apply.
///
/// # Example
///
/// ```ferron
/// match api_rules {
///     {{request.path}} ~ "^/api/"
///     {{request.method}} in "GET,POST"
/// }
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct MatchBlock {
    /// The name of this matcher (for reference).
    pub matcher: String,
    /// The list of matcher expressions.
    pub expr: Vec<MatcherExpression>,
    /// The source span of the match block.
    pub span: Span,
}

impl MatchBlock {
    /// Returns `true` if this match block contains any expressions.
    pub fn has_expressions(&self) -> bool {
        !self.expr.is_empty()
    }

    /// Returns a slice of all matcher expressions in this block.
    pub fn get_expressions(&self) -> &[MatcherExpression] {
        &self.expr
    }
}

/// A host-specific configuration block.
///
/// Host blocks apply configuration to specific hosts, protocols, or ports.
/// They are only allowed at the top level.
///
/// # Example
///
/// ```ferron
/// example.com:443 {
///     ssl enabled
/// }
///
/// *.example.com {
///     proxy http://backend
/// }
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct HostBlock {
    /// The list of host patterns this block applies to.
    pub hosts: Vec<HostPattern>,
    /// The nested block of statements.
    pub block: Block,
    /// The source span of the host block.
    pub span: Span,
}

impl HostBlock {
    /// Returns all host patterns as full strings (including protocol).
    ///
    /// # Returns
    ///
    /// A vector of strings like `"http example.com"` or `"*.example.com:443"`.
    pub fn get_host_patterns(&self) -> Vec<String> {
        self.hosts.iter().map(|hp| hp.as_full_str()).collect()
    }

    /// Checks if this host block matches a specific host pattern.
    ///
    /// # Arguments
    ///
    /// * `host` - The host string to check (e.g., `"example.com"`)
    ///
    /// # Returns
    ///
    /// `true` if any of the block's hosts match the given host.
    pub fn matches_host(&self, host: &str) -> bool {
        self.hosts.iter().any(|hp| hp.as_str() == host)
    }
}

/// A reusable configuration snippet definition.
///
/// Snippets allow defining configuration fragments that can be
/// included elsewhere (implementation-dependent).
///
/// # Example
///
/// ```ferron
/// snippet ssl_config {
///     ssl_certificate /etc/ssl/cert.pem
///     ssl_key /etc/ssl/key.pem
/// }
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct SnippetBlock {
    /// The name of this snippet.
    pub name: String,
    /// The nested block of statements.
    pub block: Block,
    /// The source span of the snippet block.
    pub span: Span,
}

/// A block of nested statements enclosed in braces.
///
/// Blocks can contain any type of statement and can be nested.
#[derive(Debug, Clone, PartialEq)]
pub struct Block {
    /// The statements contained in this block.
    pub statements: Vec<Statement>,
    /// The source span of the block (position of the opening `{`).
    pub span: Span,
}

impl Block {
    /// Returns the source span of this block.
    pub fn span(&self) -> Span {
        self.span
    }

    /// Finds all [`Directive`] statements with the given name in this block.
    ///
    /// # Arguments
    ///
    /// * `name` - The directive name to search for
    ///
    /// # Returns
    ///
    /// A vector of references to matching directives.
    pub fn find_directives(&self, name: &str) -> Vec<&Directive> {
        self.statements
            .iter()
            .filter_map(|stmt| {
                if let Statement::Directive(directive) = stmt {
                    if directive.name == name {
                        Some(directive)
                    } else {
                        None
                    }
                } else {
                    None
                }
            })
            .collect()
    }

    /// Finds the first [`Directive`] with the given name in this block.
    ///
    /// # Arguments
    ///
    /// * `name` - The directive name to search for
    ///
    /// # Returns
    ///
    /// `Some(&Directive)` if found, `None` otherwise.
    pub fn find_directive(&self, name: &str) -> Option<&Directive> {
        self.statements.iter().find_map(|stmt| {
            if let Statement::Directive(directive) = stmt {
                if directive.name == name {
                    Some(directive)
                } else {
                    None
                }
            } else {
                None
            }
        })
    }
}

/// A value in a directive argument or expression operand.
///
/// Values can be strings, numbers, booleans, or interpolated strings
/// containing variable references.
///
/// # Example
///
/// ```ferron
/// root /var/www                    # String("/var/www")
/// max_connections 1000             # Integer(1000)
/// ratio 3.14                       # Float(3.14)
/// enabled true                     # Boolean(true)
/// path "{{app.root}}/www"          # InterpolatedString(...)
/// ```
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    /// A string value (quoted or bare).
    String(String, Span),
    /// An integer value.
    Integer(i64, Span),
    /// A floating-point value.
    Float(f64, Span),
    /// A boolean value (`true` or `false`).
    Boolean(bool, Span),
    /// A string with interpolation expressions.
    InterpolatedString(Vec<StringPart>, Span),
}

impl Value {
    /// Returns the source span of this value.
    pub fn span(&self) -> Span {
        match self {
            Value::String(_, span) => *span,
            Value::Integer(_, span) => *span,
            Value::Float(_, span) => *span,
            Value::Boolean(_, span) => *span,
            Value::InterpolatedString(_, span) => *span,
        }
    }

    /// Attempts to extract this value as a string slice.
    ///
    /// # Returns
    ///
    /// `Some(&str)` if this is a `Value::String`, `None` otherwise.
    pub fn as_str(&self) -> Option<&str> {
        match self {
            Value::String(s, _) => Some(s),
            _ => None,
        }
    }

    /// Attempts to extract this value as an interpolated string slice.
    ///
    /// # Returns
    ///
    /// `Some(&[StringPart])` if this is a `Value::InterpolatedString`, `None` otherwise.
    pub fn as_interpolated_string(&self) -> Option<&[StringPart]> {
        match self {
            Value::InterpolatedString(s, _) => Some(s),
            _ => None,
        }
    }

    /// Attempts to extract this value as an integer.
    ///
    /// # Returns
    ///
    /// `Some(i64)` if this is a `Value::Integer`, `None` otherwise.
    pub fn as_i64(&self) -> Option<i64> {
        match self {
            Value::Integer(i, _) => Some(*i),
            _ => None,
        }
    }

    /// Attempts to extract this value as a float.
    ///
    /// # Returns
    ///
    /// `Some(f64)` if this is a `Value::Float`, `None` otherwise.
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            Value::Float(f, _) => Some(*f),
            _ => None,
        }
    }

    /// Attempts to extract this value as a boolean.
    ///
    /// # Returns
    ///
    /// `Some(bool)` if this is a `Value::Boolean`, `None` otherwise.
    pub fn as_bool(&self) -> Option<bool> {
        match self {
            Value::Boolean(b, _) => Some(*b),
            _ => None,
        }
    }
}

/// A part of an interpolated string.
///
/// Interpolated strings consist of alternating literal text and
/// variable expressions (e.g., `{{app.root}}`).
#[derive(Debug, Clone, PartialEq)]
pub enum StringPart {
    /// A literal string segment.
    Literal(String),
    /// A dotted path expression (e.g., `["app", "root"]` for `{{app.root}}`).
    Expression(Vec<String>),
}

impl StringPart {
    /// Converts this string part back to its source representation.
    ///
    /// # Returns
    ///
    /// For `Literal`, returns the literal text.
    /// For `Expression`, returns the path wrapped in `{{ }}`.
    pub fn as_str(&self) -> String {
        match self {
            StringPart::Literal(s) => s.clone(),
            StringPart::Expression(v) => {
                let mut expr = String::new();
                expr.push_str("{{");
                expr.push_str(&v.join("."));
                expr.push_str("}}");
                expr
            }
        }
    }
}

/// The type of host label in a host pattern.
///
/// Host patterns can be hostnames, IP addresses, or wildcards.
#[derive(Debug, Clone, PartialEq)]
pub enum HostLabels {
    /// A hostname composed of labels (e.g., `["example", "com"]`).
    Hostname(Vec<String>),
    /// An IP address (IPv4 or IPv6).
    IpAddr(IpAddr),
    /// A wildcard (`*`) matching any host.
    Wildcard,
}

impl HostLabels {
    /// Converts these labels to their string representation.
    pub fn as_str(&self) -> String {
        match self {
            HostLabels::Hostname(labels) => labels.join("."),
            HostLabels::IpAddr(ip) => ip.to_string(),
            HostLabels::Wildcard => "*".to_string(),
        }
    }
}

/// A host pattern in a host block.
///
/// Host patterns specify which hosts a configuration block applies to.
/// They can include a protocol, hostname/IP, and optional port.
///
/// # Examples
///
/// - `example.com` - hostname only
/// - `*.example.com:443` - wildcard with port
/// - `http://api.example.com` - with protocol
/// - `[2001:db8::1]:8080` - IPv6 with port
#[derive(Debug, Clone, PartialEq)]
pub struct HostPattern {
    /// The host labels (hostname, IP, or wildcard).
    pub labels: HostLabels,
    /// The optional port number.
    pub port: Option<u16>,
    /// The optional protocol (e.g., `"http"`, `"tcp"`).
    pub protocol: Option<String>,
    /// The source span of the host pattern.
    pub span: Span,
}

impl HostPattern {
    /// Returns the host pattern as a string (without protocol).
    ///
    /// # Example
    ///
    /// ```rust
    /// # use ferronconf::Config;
    /// # use std::str::FromStr;
    /// let config = Config::from_str("example.com:443 { }").unwrap();
    /// if let Some(hb) = config.find_host_blocks().first() {
    ///     assert_eq!(hb.hosts[0].as_str(), "example.com:443");
    /// }
    /// ```
    pub fn as_str(&self) -> String {
        let mut pattern = self.labels.as_str();
        if let Some(port) = self.port {
            pattern.push(':');
            pattern.push_str(&port.to_string());
        }
        pattern
    }

    /// Returns the full host pattern including the protocol.
    ///
    /// # Example
    ///
    /// ```rust
    /// # use ferronconf::Config;
    /// # use std::str::FromStr;
    /// let config = Config::from_str("http example.com { }").unwrap();
    /// if let Some(hb) = config.find_host_blocks().first() {
    ///     assert_eq!(hb.hosts[0].as_full_str(), "http example.com");
    /// }
    /// ```
    pub fn as_full_str(&self) -> String {
        let mut pattern = String::new();
        if let Some(protocol) = &self.protocol {
            pattern.push_str(protocol);
            pattern.push(' ');
        }
        pattern.push_str(&self.as_str());
        pattern
    }
}

/// An expression in a match block that compares two operands.
///
/// # Example
///
/// ```ferron
/// {{request.path}} ~ "^/api/"
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct MatcherExpression {
    /// The left-hand side operand.
    pub left: Operand,
    /// The comparison operator.
    pub op: Operator,
    /// The right-hand side operand.
    pub right: Operand,
    /// The source span of the expression.
    pub span: Span,
}

impl MatcherExpression {
    /// Returns the source span of this expression.
    pub fn span(&self) -> Span {
        self.span
    }

    /// Returns `true` if this is an equality (`==`) expression.
    pub fn is_equality(&self) -> bool {
        matches!(self.op, Operator::Eq)
    }

    /// Returns `true` if this is an inequality (`!=`) expression.
    pub fn is_inequality(&self) -> bool {
        matches!(self.op, Operator::NotEq)
    }

    /// Returns `true` if this is a regex (`~` or `!~`) expression.
    pub fn is_regex(&self) -> bool {
        matches!(self.op, Operator::Regex | Operator::NotRegex)
    }
}

/// An operand in a matcher expression.
///
/// Operands can be identifier paths, strings, or numbers.
#[derive(Debug, Clone, PartialEq)]
pub enum Operand {
    /// A dotted identifier path (e.g., `["request", "path"]` for `{{request.path}}`).
    Identifier(Vec<String>, Span),
    /// A string value.
    String(String, Span),
    /// An integer value.
    Integer(i64, Span),
    /// A floating-point value.
    Float(f64, Span),
}

impl Operand {
    /// Returns the source span of this operand.
    pub fn span(&self) -> Span {
        match self {
            Operand::Identifier(_, span) => *span,
            Operand::String(_, span) => *span,
            Operand::Integer(_, span) => *span,
            Operand::Float(_, span) => *span,
        }
    }

    /// Attempts to extract this operand as a string slice.
    ///
    /// # Returns
    ///
    /// `Some(&str)` if this is an `Operand::String`, `None` otherwise.
    pub fn as_str(&self) -> Option<&str> {
        match self {
            Operand::String(s, _) => Some(s),
            _ => None,
        }
    }

    /// Attempts to extract this operand as an identifier path.
    ///
    /// # Returns
    ///
    /// `Some(&[String])` if this is an `Operand::Identifier`, `None` otherwise.
    pub fn as_identifier(&self) -> Option<&[String]> {
        match self {
            Operand::Identifier(parts, _) => Some(parts),
            _ => None,
        }
    }

    /// Attempts to extract this operand as an integer.
    ///
    /// # Returns
    ///
    /// `Some(i64)` if this is an `Operand::Integer`, `None` otherwise.
    pub fn as_i64(&self) -> Option<i64> {
        match self {
            Operand::Integer(i, _) => Some(*i),
            _ => None,
        }
    }

    /// Attempts to extract this operand as a float.
    ///
    /// # Returns
    ///
    /// `Some(f64)` if this is an `Operand::Float`, `None` otherwise.
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            Operand::Float(f, _) => Some(*f),
            _ => None,
        }
    }
}

/// A comparison operator in a matcher expression.
///
/// # Operators
///
/// | Variant | Syntax | Description |
/// |---------|--------|-------------|
/// | `Eq` | `==` | Equality comparison |
/// | `NotEq` | `!=` | Inequality comparison |
/// | `Regex` | `~` | Regular expression match |
/// | `NotRegex` | `!~` | Negated regex match |
/// | `In` | `in` | Membership test |
#[derive(Debug, Clone, PartialEq)]
pub enum Operator {
    /// Equality (`==`).
    Eq,
    /// Inequality (`!=`).
    NotEq,
    /// Regex match (`~`).
    Regex,
    /// Negated regex match (`!~`).
    NotRegex,
    /// Membership test (`in`).
    In,
}

impl Operator {
    /// Returns the string representation of this operator.
    pub fn as_str(&self) -> &'static str {
        match self {
            Operator::Eq => "==",
            Operator::NotEq => "!=",
            Operator::Regex => "~",
            Operator::NotRegex => "!~",
            Operator::In => "in",
        }
    }

    /// Returns `true` if this is a comparison operator (`==` or `!=`).
    pub fn is_comparison(&self) -> bool {
        matches!(self, Operator::Eq | Operator::NotEq)
    }

    /// Returns `true` if this is a regex operator (`~` or `!~`).
    pub fn is_regex(&self) -> bool {
        matches!(self, Operator::Regex | Operator::NotRegex)
    }
}