libhaystack 3.1.5

Rust implementation of the Haystack 4 data types, defs, filter, units, and encodings
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
// Copyright (C) 2020 - 2022, J2 Innovations

//! Haystack filter builder
//!
//! Provides a fluent, type-safe API for constructing [`Filter`](crate::filter::Filter) values
//! by building the filter's AST directly — no intermediate string parsing required.
//!
//! # Examples
//!
//! ```
//! use libhaystack::filter::{Filter, FilterBuilder};
//! use libhaystack::val::Value;
//!
//! // site and dis == "Main"
//! let filter = FilterBuilder::new()
//!     .has("site")
//!     .and()
//!     .eq("dis", Value::make_str("Main"))
//!     .build();
//!
//! assert_eq!(filter.to_string(), r#"site and dis == "Main""#);
//! ```
//!
//! ```
//! use libhaystack::filter::FilterBuilder;
//! use libhaystack::val::Value;
//!
//! // (equip or point) and siteRef == @mySite
//! let filter = FilterBuilder::new()
//!     .start_parens()
//!         .has("equip")
//!         .or()
//!         .has("point")
//!     .end_parens()
//!     .and()
//!     .eq("siteRef", Value::make_ref("mySite"))
//!     .build();
//!
//! assert_eq!(filter.to_string(), "(equip or point) and siteRef == @mySite");
//! ```

use super::Filter;
use super::nodes::{And, Cmp, CmpOp, Has, IsA, Missing, Or, Parens, Relation, Term, WildcardEq};
use super::path::Path;
use crate::haystack::encoding::zinc::decode::id::Id;
use crate::haystack::val::{Ref, Symbol, Value};
use std::marker::PhantomData;

/// Converts a value into a filter [`Path`].
///
/// Implemented for `&str` (single segment) and `&[&str]` / arrays (multi-segment).
pub trait IntoFilterPath {
    fn into_filter_path(self) -> Path;
}

impl IntoFilterPath for &str {
    fn into_filter_path(self) -> Path {
        Path::from(self)
    }
}

impl IntoFilterPath for &[&str] {
    fn into_filter_path(self) -> Path {
        Path::from(self.iter().map(|s| Id::from(*s)).collect::<Vec<_>>())
    }
}

impl<const N: usize> IntoFilterPath for [&str; N] {
    fn into_filter_path(self) -> Path {
        Path::from(self.iter().map(|s| Id::from(*s)).collect::<Vec<_>>())
    }
}

impl IntoFilterPath for Vec<&str> {
    fn into_filter_path(self) -> Path {
        Path::from(self.iter().map(|s| Id::from(*s)).collect::<Vec<_>>())
    }
}

/// Typestate marker for [`FilterBuilder`] — a term must be added next.
///
/// Term-producing methods ([`has`](FilterBuilder::has), [`eq`](FilterBuilder::eq),
/// [`is_a`](FilterBuilder::is_a), …) are available. Connectives (`and`, `or`),
/// `end_parens`, and `build` are **not** available until a term has been added.
pub struct NeedsTerm;

/// Typestate marker for [`FilterBuilder`] — a term has just been added.
///
/// Connectives ([`and`](FilterBuilder::and), [`or`](FilterBuilder::or)),
/// [`end_parens`](FilterBuilder::end_parens), and [`build`](FilterBuilder::build)
/// are available. To add another term, call `and()` or `or()` first to return
/// to the [`NeedsTerm`] state.
pub struct HasTerm;

/// Fluent builder for constructing a Haystack [`Filter`] via its AST.
///
/// Uses a [typestate] pattern to prevent invalid filter sequences at compile
/// time. The builder starts in the [`NeedsTerm`] state; only term-producing
/// methods are available. After adding a term the builder transitions to
/// [`HasTerm`], where `and`, `or`, `end_parens`, and `build` become available.
///
/// # Invalid sequences that do not compile
///
/// ```compile_fail
/// # use libhaystack::filter::FilterBuilder;
/// // cannot call or() without a preceding term
/// let _ = FilterBuilder::new().or();
/// ```
///
/// ```compile_fail
/// # use libhaystack::filter::FilterBuilder;
/// // cannot call or() twice in a row
/// let _ = FilterBuilder::new().has("site").or().or();
/// ```
///
/// ```compile_fail
/// # use libhaystack::filter::FilterBuilder;
/// // cannot build an empty filter
/// let _ = FilterBuilder::new().build();
/// ```
///
/// ```compile_fail
/// # use libhaystack::filter::FilterBuilder;
/// // consecutive terms require an explicit and() or or() between them
/// let _ = FilterBuilder::new().has("site").has("equip").build();
/// ```
///
/// # Path arguments
///
/// Any method that accepts a `path` parameter can receive:
/// - A single `&str` tag name: `"site"`
/// - A slice of `&str` for indirection: `["siteRef", "dis"]`
/// - A fixed-size array: `["equipRef", "siteRef"]`
///
/// # Value arguments
///
/// Comparison methods accept a [`Value`] directly. Use the `Value::make_*` helpers
/// (e.g. [`Value::make_str`], [`Value::make_number`], [`Value::make_ref`]) to construct
/// the right type.
///
/// [typestate]: https://cliffle.com/blog/rust-typestate/
pub struct FilterBuilder<S = NeedsTerm> {
    _state: PhantomData<S>,
    /// Completed `And` nodes for the top-level `Or`.
    ands: Vec<And>,
    /// Terms accumulating for the current `And` node.
    current_terms: Vec<Term>,
    /// Stack of saved `(ands, current_terms)` states pushed by `start_parens`.
    paren_stack: Vec<(Vec<And>, Vec<Term>)>,
}

impl FilterBuilder<NeedsTerm> {
    /// Create a new, empty [`FilterBuilder`] in the [`NeedsTerm`] state.
    pub fn new() -> Self {
        Self::default()
    }
}

impl Default for FilterBuilder<NeedsTerm> {
    fn default() -> Self {
        FilterBuilder {
            _state: PhantomData,
            ands: Vec::new(),
            current_terms: Vec::new(),
            paren_stack: Vec::new(),
        }
    }
}

// ---------------------------------------------------------------------------
// Private helpers (available in any state)
// ---------------------------------------------------------------------------

impl<S> FilterBuilder<S> {
    /// Reinterprets the builder's state marker without moving any data.
    fn transition<T>(self) -> FilterBuilder<T> {
        FilterBuilder {
            _state: PhantomData,
            ands: self.ands,
            current_terms: self.current_terms,
            paren_stack: self.paren_stack,
        }
    }

    /// Finalises the accumulated terms into an `And` node and clears `current_terms`.
    /// Does nothing if `current_terms` is empty.
    fn flush_and(&mut self) {
        if !self.current_terms.is_empty() {
            self.ands.push(And {
                terms: std::mem::take(&mut self.current_terms),
            });
        }
    }

    fn cmp(mut self, path: impl IntoFilterPath, op: CmpOp, value: Value) -> FilterBuilder<HasTerm> {
        self.current_terms.push(Term::Cmp(Cmp {
            path: path.into_filter_path(),
            op,
            value,
        }));
        self.transition()
    }
}

// ---------------------------------------------------------------------------
// Methods available only when a term is needed (NeedsTerm state)
// ---------------------------------------------------------------------------

impl FilterBuilder<NeedsTerm> {
    // -------------------------------------------------------------------------
    // Parentheses
    // -------------------------------------------------------------------------

    /// Opens a parenthesised sub-expression.
    ///
    /// The current builder state is saved onto an internal stack; subsequent
    /// method calls accumulate terms for the sub-expression until
    /// [`end_parens`](FilterBuilder::end_parens) is called.
    ///
    /// ```
    /// # use libhaystack::filter::FilterBuilder;
    /// let f = FilterBuilder::new()
    ///     .start_parens()
    ///         .has("site")
    ///         .or()
    ///         .has("equip")
    ///     .end_parens()
    ///     .build();
    /// assert_eq!(f.to_string(), "(site or equip)");
    /// ```
    pub fn start_parens(mut self) -> FilterBuilder<NeedsTerm> {
        self.paren_stack.push((
            std::mem::take(&mut self.ands),
            std::mem::take(&mut self.current_terms),
        ));
        self.transition()
    }

    // -------------------------------------------------------------------------
    // Terminal terms
    // -------------------------------------------------------------------------

    /// Adds a *has* condition — the tag at `path` must exist.
    ///
    /// Equivalent to the bare path form in filter syntax: `site`.
    ///
    /// ```
    /// # use libhaystack::filter::FilterBuilder;
    /// let f = FilterBuilder::new().has("site").build();
    /// assert_eq!(f.to_string(), "site");
    /// ```
    pub fn has(mut self, path: impl IntoFilterPath) -> FilterBuilder<HasTerm> {
        self.current_terms.push(Term::Has(Has {
            path: path.into_filter_path(),
        }));
        self.transition()
    }

    /// Adds a *missing* condition — the tag at `path` must **not** exist.
    ///
    /// Equivalent to `not path` in filter syntax.
    ///
    /// ```
    /// # use libhaystack::filter::FilterBuilder;
    /// let f = FilterBuilder::new().not("site").build();
    /// assert_eq!(f.to_string(), "not site");
    /// ```
    pub fn not(mut self, path: impl IntoFilterPath) -> FilterBuilder<HasTerm> {
        self.current_terms.push(Term::Missing(Missing {
            path: path.into_filter_path(),
        }));
        self.transition()
    }

    /// Adds an *is-a* condition using a type symbol.
    ///
    /// Equivalent to `^symbol` in filter syntax.
    ///
    /// ```
    /// # use libhaystack::filter::FilterBuilder;
    /// # use libhaystack::val::Symbol;
    /// let f = FilterBuilder::new().is_a("point").build();
    /// assert_eq!(f.to_string(), "^point");
    /// ```
    pub fn is_a(mut self, symbol: impl Into<Symbol>) -> FilterBuilder<HasTerm> {
        self.current_terms.push(Term::IsA(IsA {
            symbol: symbol.into(),
        }));
        self.transition()
    }

    /// Adds a *wildcard equality* condition.
    ///
    /// Follows references at `id` until the target matches `ref_value`.
    /// Equivalent to `id *== @ref` in filter syntax.
    ///
    /// ```
    /// # use libhaystack::filter::FilterBuilder;
    /// # use libhaystack::val::Ref;
    /// let f = FilterBuilder::new()
    ///     .wildcard_eq("siteRef", Ref::from("mySite"))
    ///     .build();
    /// assert_eq!(f.to_string(), "siteRef *== @mySite");
    /// ```
    pub fn wildcard_eq(
        mut self,
        id: impl IntoFilterPath,
        ref_value: Ref,
    ) -> FilterBuilder<HasTerm> {
        self.current_terms.push(Term::WildcardEq(WildcardEq {
            id: id.into_filter_path(),
            ref_value,
        }));
        self.transition()
    }

    /// Adds a *relationship* condition.
    ///
    /// - `rel` – the relationship symbol name (e.g. `"containedBy"`)
    /// - `term` – optional relationship term symbol
    /// - `ref_value` – optional target reference
    ///
    /// Equivalent to `rel?`, `rel? ^term`, or `rel? ^term @ref` in filter syntax.
    ///
    /// ```
    /// # use libhaystack::filter::FilterBuilder;
    /// # use libhaystack::val::{Symbol, Ref};
    /// let f = FilterBuilder::new()
    ///     .relation("containedBy", None, Some(Ref::from("mySite")))
    ///     .build();
    /// assert_eq!(f.to_string(), "containedBy? @mySite");
    /// ```
    pub fn relation(
        mut self,
        rel: impl Into<Symbol>,
        term: Option<Symbol>,
        ref_value: Option<Ref>,
    ) -> FilterBuilder<HasTerm> {
        self.current_terms.push(Term::Relation(Relation {
            rel: rel.into(),
            rel_term: term,
            ref_value,
        }));
        self.transition()
    }

    // -------------------------------------------------------------------------
    // Comparison operators
    // -------------------------------------------------------------------------

    /// Adds an equality comparison: `path == value`.
    ///
    /// ```
    /// # use libhaystack::filter::FilterBuilder;
    /// # use libhaystack::val::Value;
    /// let f = FilterBuilder::new().eq("dis", Value::make_str("Chiller")).build();
    /// assert_eq!(f.to_string(), r#"dis == "Chiller""#);
    /// ```
    pub fn eq(self, path: impl IntoFilterPath, value: Value) -> FilterBuilder<HasTerm> {
        self.cmp(path, CmpOp::Eq, value)
    }

    /// Adds a not-equal comparison: `path != value`.
    pub fn ne(self, path: impl IntoFilterPath, value: Value) -> FilterBuilder<HasTerm> {
        self.cmp(path, CmpOp::NotEq, value)
    }

    /// Adds a less-than comparison: `path < value`.
    pub fn lt(self, path: impl IntoFilterPath, value: Value) -> FilterBuilder<HasTerm> {
        self.cmp(path, CmpOp::LessThan, value)
    }

    /// Adds a less-than-or-equal comparison: `path <= value`.
    pub fn lte(self, path: impl IntoFilterPath, value: Value) -> FilterBuilder<HasTerm> {
        self.cmp(path, CmpOp::LessThanEq, value)
    }

    /// Adds a greater-than comparison: `path > value`.
    pub fn gt(self, path: impl IntoFilterPath, value: Value) -> FilterBuilder<HasTerm> {
        self.cmp(path, CmpOp::GreatThan, value)
    }

    /// Adds a greater-than-or-equal comparison: `path >= value`.
    pub fn gte(self, path: impl IntoFilterPath, value: Value) -> FilterBuilder<HasTerm> {
        self.cmp(path, CmpOp::GreatThanEq, value)
    }

    // -------------------------------------------------------------------------
    // Sub-filter embedding
    // -------------------------------------------------------------------------

    /// Embeds an existing [`Filter`] as a parenthesised sub-expression.
    ///
    /// This is useful for composing pre-built filters together.
    ///
    /// ```
    /// # use libhaystack::filter::{Filter, FilterBuilder};
    /// # use libhaystack::val::Value;
    /// let inner = Filter::try_from("equip or point").unwrap();
    /// let f = FilterBuilder::new()
    ///     .filter(inner)
    ///     .and()
    ///     .has("site")
    ///     .build();
    /// assert_eq!(f.to_string(), "(equip or point) and site");
    /// ```
    pub fn filter(mut self, filter: Filter) -> FilterBuilder<HasTerm> {
        self.current_terms
            .push(Term::Parens(Parens { or: filter.or }));
        self.transition()
    }
}

// ---------------------------------------------------------------------------
// Methods available only when a term has been added
// ---------------------------------------------------------------------------

impl FilterBuilder<HasTerm> {
    // -------------------------------------------------------------------------
    // Logical connectives
    // -------------------------------------------------------------------------

    /// Transitions to the [`NeedsTerm`] state, requiring an explicit term next.
    ///
    /// In the AST, consecutive terms are implicitly and-ed; this method signals
    /// that intent at the type level and prevents accidental omissions.
    ///
    /// ```
    /// # use libhaystack::filter::FilterBuilder;
    /// let f = FilterBuilder::new().has("site").and().has("equip").build();
    /// assert_eq!(f.to_string(), "site and equip");
    /// ```
    pub fn and(self) -> FilterBuilder<NeedsTerm> {
        self.transition()
    }

    /// Finalises the current `and`-group and starts a new one, producing an `or` in the output.
    ///
    /// ```
    /// # use libhaystack::filter::FilterBuilder;
    /// let f = FilterBuilder::new().has("site").or().has("equip").build();
    /// assert_eq!(f.to_string(), "site or equip");
    /// ```
    pub fn or(mut self) -> FilterBuilder<NeedsTerm> {
        self.flush_and();
        self.transition()
    }

    // -------------------------------------------------------------------------
    // Parentheses
    // -------------------------------------------------------------------------

    /// Closes the current parenthesised sub-expression and adds it as a single
    /// [`Parens`] term in the outer context.
    ///
    /// If called without a matching [`start_parens`](FilterBuilder::start_parens)
    /// the call is silently ignored and the builder state is left unchanged.
    pub fn end_parens(mut self) -> FilterBuilder<HasTerm> {
        if let Some((outer_ands, outer_terms)) = self.paren_stack.pop() {
            self.flush_and();
            let inner_or = Or {
                ands: std::mem::take(&mut self.ands),
            };
            self.ands = outer_ands;
            self.current_terms = outer_terms;
            self.current_terms
                .push(Term::Parens(Parens { or: inner_or }));
        }
        self
    }

    // -------------------------------------------------------------------------
    // Build
    // -------------------------------------------------------------------------

    /// Consumes the builder and returns the constructed [`Filter`].
    ///
    /// Any open `and`-group is automatically finalised.
    pub fn build(mut self) -> Filter {
        self.flush_and();
        Filter {
            or: Or { ands: self.ands },
        }
    }
}

/// Convert a [`FilterBuilder<HasTerm>`] into a [`Filter`].
///
/// Enforces at compile time that only a builder with at least one term can be
/// converted — an empty builder (in [`NeedsTerm`] state) does not implement this.
impl From<FilterBuilder<HasTerm>> for Filter {
    fn from(builder: FilterBuilder<HasTerm>) -> Self {
        builder.build()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::val::{Date, Ref, Time, Value};

    // ------------------------------------------------------------------
    // has / not
    // ------------------------------------------------------------------

    #[test]
    fn test_builder_has() {
        let f = FilterBuilder::new().has("site").build();
        assert_eq!(f.to_string(), "site");
    }

    #[test]
    fn test_builder_not() {
        let f = FilterBuilder::new().not("site").build();
        assert_eq!(f.to_string(), "not site");
    }

    // ------------------------------------------------------------------
    // and / or
    // ------------------------------------------------------------------

    #[test]
    fn test_builder_and() {
        let f = FilterBuilder::new().has("site").and().has("equip").build();
        assert_eq!(f.to_string(), "site and equip");
    }

    #[test]
    fn test_builder_or() {
        let f = FilterBuilder::new().has("site").or().has("equip").build();
        assert_eq!(f.to_string(), "site or equip");
    }

    #[test]
    fn test_builder_and_or_combined() {
        // site and equip or point
        let f = FilterBuilder::new()
            .has("site")
            .and()
            .has("equip")
            .or()
            .has("point")
            .build();
        assert_eq!(f.to_string(), "site and equip or point");
    }

    // ------------------------------------------------------------------
    // is_a
    // ------------------------------------------------------------------

    #[test]
    fn test_builder_is_a() {
        let f = FilterBuilder::new().is_a("point").build();
        assert_eq!(f.to_string(), "^point");
    }

    // ------------------------------------------------------------------
    // Parentheses
    // ------------------------------------------------------------------

    #[test]
    fn test_builder_parens() {
        let f = FilterBuilder::new()
            .start_parens()
            .has("equip")
            .or()
            .has("point")
            .end_parens()
            .build();
        assert_eq!(f.to_string(), "(equip or point)");
    }

    #[test]
    fn test_builder_parens_with_outer_term() {
        let f = FilterBuilder::new()
            .start_parens()
            .has("equip")
            .or()
            .has("point")
            .end_parens()
            .and()
            .eq("siteRef", Value::make_ref("mySite"))
            .build();
        assert_eq!(f.to_string(), "(equip or point) and siteRef == @mySite");
    }

    // ------------------------------------------------------------------
    // Comparison operators
    // ------------------------------------------------------------------

    #[test]
    fn test_builder_eq_str() {
        let f = FilterBuilder::new()
            .eq("dis", Value::make_str("Chiller"))
            .build();
        assert_eq!(f.to_string(), r#"dis == "Chiller""#);
    }

    #[test]
    fn test_builder_eq_number() {
        let f = FilterBuilder::new()
            .eq("num", Value::make_number(42.0))
            .build();
        assert_eq!(f.to_string(), "num == 42");
    }

    #[test]
    fn test_builder_ne() {
        let f = FilterBuilder::new()
            .ne("num", Value::make_number(42.0))
            .build();
        assert_eq!(f.to_string(), "num != 42");
    }

    #[test]
    fn test_builder_lt() {
        let f = FilterBuilder::new()
            .lt("num", Value::make_number(10.0))
            .build();
        assert_eq!(f.to_string(), "num < 10");
    }

    #[test]
    fn test_builder_lte() {
        let f = FilterBuilder::new()
            .lte("num", Value::make_number(10.0))
            .build();
        assert_eq!(f.to_string(), "num <= 10");
    }

    #[test]
    fn test_builder_gt() {
        let f = FilterBuilder::new()
            .gt("num", Value::make_number(10.0))
            .build();
        assert_eq!(f.to_string(), "num > 10");
    }

    #[test]
    fn test_builder_gte() {
        let f = FilterBuilder::new()
            .gte("num", Value::make_number(10.0))
            .build();
        assert_eq!(f.to_string(), "num >= 10");
    }

    #[test]
    fn test_builder_eq_ref() {
        let f = FilterBuilder::new()
            .eq("siteRef", Value::make_ref("mySite"))
            .build();
        assert_eq!(f.to_string(), "siteRef == @mySite");
    }

    #[test]
    fn test_builder_eq_bool() {
        let f = FilterBuilder::new()
            .eq("active", Value::make_bool(true))
            .build();
        assert_eq!(f.to_string(), "active == true");
    }

    #[test]
    fn test_builder_eq_date() {
        let date = Date::from_ymd(2024, 3, 15).expect("date");
        let f = FilterBuilder::new()
            .eq("lastMod", Value::make_date(date))
            .build();
        assert_eq!(f.to_string(), "lastMod == 2024-03-15");
    }

    #[test]
    fn test_builder_eq_time() {
        let time = Time::from_hms(12, 30, 0).expect("time");
        let f = FilterBuilder::new()
            .eq("startTime", Value::make_time(time))
            .build();
        assert_eq!(f.to_string(), "startTime == 12:30:00");
    }

    // ------------------------------------------------------------------
    // Multi-segment paths
    // ------------------------------------------------------------------

    #[test]
    fn test_builder_multi_segment_path_has() {
        let f = FilterBuilder::new().has(["siteRef", "dis"]).build();
        assert_eq!(f.to_string(), "siteRef->dis");
    }

    #[test]
    fn test_builder_multi_segment_path_eq() {
        let f = FilterBuilder::new()
            .eq(["siteRef", "dis"], Value::make_str("Main"))
            .build();
        assert_eq!(f.to_string(), r#"siteRef->dis == "Main""#);
    }

    // ------------------------------------------------------------------
    // wildcard_eq
    // ------------------------------------------------------------------

    #[test]
    fn test_builder_wildcard_eq() {
        let f = FilterBuilder::new()
            .wildcard_eq("siteRef", Ref::from("mySite"))
            .build();
        assert_eq!(f.to_string(), "siteRef *== @mySite");
    }

    // ------------------------------------------------------------------
    // relation
    // ------------------------------------------------------------------

    #[test]
    fn test_builder_relation_simple() {
        let f = FilterBuilder::new()
            .relation("containedBy", None, None)
            .build();
        assert_eq!(f.to_string(), "containedBy?");
    }

    #[test]
    fn test_builder_relation_with_ref() {
        let f = FilterBuilder::new()
            .relation("containedBy", None, Some(Ref::from("mySite")))
            .build();
        assert_eq!(f.to_string(), "containedBy? @mySite");
    }

    #[test]
    fn test_builder_relation_with_term_and_ref() {
        let f = FilterBuilder::new()
            .relation(
                "containedBy",
                Some(Symbol::from("site")),
                Some(Ref::from("mySite")),
            )
            .build();
        assert_eq!(f.to_string(), "containedBy? ^site @mySite");
    }

    // ------------------------------------------------------------------
    // Embedded filter
    // ------------------------------------------------------------------

    #[test]
    fn test_builder_embed_filter() {
        let inner = Filter::try_from("equip or point").unwrap();
        let f = FilterBuilder::new().filter(inner).and().has("site").build();
        assert_eq!(f.to_string(), "(equip or point) and site");
    }

    // ------------------------------------------------------------------
    // From<FilterBuilder> for Filter
    // ------------------------------------------------------------------

    #[test]
    fn test_filter_from_builder() {
        let builder: FilterBuilder<HasTerm> = FilterBuilder::new().has("site").and().has("equip");
        let f: Filter = builder.into();
        assert_eq!(f.to_string(), "site and equip");
    }

    // ------------------------------------------------------------------
    // Round-trip: builder output matches parser output
    // ------------------------------------------------------------------

    #[test]
    fn test_builder_round_trip_simple() {
        let built = FilterBuilder::new()
            .has("site")
            .and()
            .eq("dis", Value::make_str("Test"))
            .build();
        let parsed = Filter::try_from(r#"site and dis == "Test""#).unwrap();
        assert_eq!(built.to_string(), parsed.to_string());
    }

    #[test]
    fn test_builder_round_trip_parens() {
        let built = FilterBuilder::new()
            .start_parens()
            .has("equip")
            .or()
            .has("point")
            .end_parens()
            .and()
            .has("site")
            .build();
        let parsed = Filter::try_from("(equip or point) and site").unwrap();
        assert_eq!(built.to_string(), parsed.to_string());
    }

    #[test]
    fn test_builder_round_trip_or() {
        let built = FilterBuilder::new()
            .has("site")
            .or()
            .has("equip")
            .or()
            .has("point")
            .build();
        let parsed = Filter::try_from("site or equip or point").unwrap();
        assert_eq!(built.to_string(), parsed.to_string());
    }
}