hedl-core 2.0.0

Core parser and data model for HEDL (Hierarchical Entity Data Language)
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
// Dweve HEDL - Hierarchical Entity Data Language
//
// Copyright (c) 2025 Dweve IP B.V. and individual contributors.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License in the LICENSE file at the
// root of this repository or at: http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Reference resolution for HEDL.

use crate::document::{Document, Item, MatrixList, Node};
use crate::error::{HedlError, HedlResult};
use crate::limits::Limits;
use crate::value::Value;
use std::collections::{BTreeMap, HashMap};

/// Reference resolution mode for controlling validation behavior.
///
/// Determines how the reference resolver handles unresolved or problematic references.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ReferenceMode {
    /// Strict mode: Unresolved references cause errors.
    ///
    /// This is the default and recommended mode for production parsing.
    /// Any reference that cannot be resolved will result in a parse error.
    ///
    /// # Behavior
    /// - Unresolved references → Error
    /// - Ambiguous references → Error (always, regardless of mode)
    #[default]
    Strict,

    /// Lenient mode: Unresolved references are ignored.
    ///
    /// Useful for partial parsing, work-in-progress documents, or when
    /// reference validation is deferred to a separate validation pass.
    ///
    /// # Behavior
    /// - Unresolved references → Silently ignored
    /// - Ambiguous references → Error (always, regardless of mode)
    ///
    /// # Use Cases
    /// - Parsing incomplete documents during development
    /// - Incremental parsing where not all nodes are loaded
    /// - Custom validation workflows
    Lenient,
}

impl ReferenceMode {
    /// Returns true if this mode should fail on unresolved references.
    #[inline]
    pub fn is_strict(self) -> bool {
        matches!(self, ReferenceMode::Strict)
    }

    /// Returns true if this mode allows unresolved references.
    #[inline]
    pub fn is_lenient(self) -> bool {
        matches!(self, ReferenceMode::Lenient)
    }
}

impl From<bool> for ReferenceMode {
    /// Convert from legacy boolean parameter.
    ///
    /// `true` → `Strict`, `false` → `Lenient`
    fn from(strict: bool) -> Self {
        if strict {
            ReferenceMode::Strict
        } else {
            ReferenceMode::Lenient
        }
    }
}

/// Type registries with both forward and inverted indices for efficient lookups.
///
/// P0 OPTIMIZATION: Inverted index for unqualified references (100-1000x speedup)
/// - Forward index: type -> (id -> line_num) for qualified lookups (O(log n))
/// - Inverted index: id -> \[types\] for unqualified lookups (O(1))
pub struct TypeRegistry {
    /// Forward index: type_name -> (id -> line_number)
    by_type: BTreeMap<String, BTreeMap<String, usize>>,
    /// Inverted index: id -> list of type names containing that ID
    by_id: HashMap<String, Vec<String>>,
    /// Total number of IDs registered across all types (for limit enforcement)
    total_ids: usize,
}

impl TypeRegistry {
    /// Create a new empty registry
    pub fn new() -> Self {
        Self {
            by_type: BTreeMap::new(),
            by_id: HashMap::new(),
            total_ids: 0,
        }
    }

    /// Register an ID in a type, maintaining both indices
    pub fn register(
        &mut self,
        type_name: &str,
        id: &str,
        line_num: usize,
        limits: &Limits,
    ) -> HedlResult<()> {
        let type_registry = self.by_type.entry(type_name.to_string()).or_default();

        if let Some(&prev_line) = type_registry.get(id) {
            return Err(HedlError::collision(
                format!(
                    "duplicate ID '{}' in type '{}', previously defined at line {}",
                    id, type_name, prev_line
                ),
                line_num,
            ));
        }

        // Check total IDs limit before registration
        if self.total_ids >= limits.max_total_ids {
            return Err(HedlError::security(
                format!(
                    "total ID registrations {} exceeds limit {}",
                    self.total_ids, limits.max_total_ids
                ),
                line_num,
            ));
        }

        type_registry.insert(id.to_string(), line_num);

        // Update inverted index
        self.by_id
            .entry(id.to_string())
            .or_default()
            .push(type_name.to_string());

        // Increment total count
        self.total_ids += 1;

        Ok(())
    }

    /// Look up ID in a specific type (qualified reference)
    pub fn contains_in_type(&self, type_name: &str, id: &str) -> bool {
        self.by_type
            .get(type_name)
            .map(|r| r.contains_key(id))
            .unwrap_or(false)
    }

    /// Look up ID across all types (unqualified reference)
    /// Returns list of types containing this ID
    pub fn lookup_unqualified(&self, id: &str) -> Option<&[String]> {
        self.by_id.get(id).map(|v| v.as_slice())
    }

    /// Iterate over all IDs and their associated types in the inverted index.
    ///
    /// Used for merging registries during parallel parsing.
    pub fn by_id_iter(&self) -> impl Iterator<Item = (&String, &Vec<String>)> {
        self.by_id.iter()
    }
}

impl Default for TypeRegistry {
    fn default() -> Self {
        Self::new()
    }
}

/// Check NEST hierarchy depth against security limit.
///
/// Returns an error if the depth exceeds the maximum allowed depth.
fn check_nest_depth(depth: usize, max_depth: usize) -> HedlResult<()> {
    if depth > max_depth {
        return Err(HedlError::security(
            format!(
                "NEST hierarchy depth {} exceeds maximum allowed depth {}",
                depth, max_depth
            ),
            0,
        ));
    }
    Ok(())
}

/// Register a node ID, checking for collisions.
pub fn register_node(
    registries: &mut TypeRegistry,
    type_name: &str,
    id: &str,
    line_num: usize,
    limits: &Limits,
) -> HedlResult<()> {
    registries.register(type_name, id, line_num, limits)
}

/// Resolve all references in a document using default limits.
///
/// Validates that all references point to existing nodes according to the
/// specified reference resolution mode.
///
/// # Arguments
/// - `doc`: The document to validate
/// - `mode`: Reference resolution mode (strict or lenient)
///
/// # Errors
/// - In strict mode: Returns error if any reference cannot be resolved
/// - In any mode: Returns error if any reference is ambiguous
/// - Returns error if nesting depth exceeds limits
///
/// # Examples
/// ```
/// use hedl_core::{Document, ReferenceMode, resolve_references};
///
/// let doc = Document::new((2, 0));
/// // Strict mode - fail on unresolved references
/// resolve_references(&doc, ReferenceMode::Strict)?;
///
/// // Lenient mode - ignore unresolved references
/// resolve_references(&doc, ReferenceMode::Lenient)?;
/// # Ok::<(), hedl_core::HedlError>(())
/// ```
pub fn resolve_references(doc: &Document, mode: ReferenceMode) -> HedlResult<()> {
    resolve_references_with_limits(doc, mode, &Limits::default())
}

/// Resolve all references in a document with configurable limits.
///
/// Validates that all references point to existing nodes according to the
/// specified reference resolution mode, using custom security limits.
///
/// # Arguments
/// - `doc`: The document to validate
/// - `mode`: Reference resolution mode (strict or lenient)
/// - `limits`: Security limits for parsing
///
/// # Errors
/// - In strict mode: Returns error if any reference cannot be resolved
/// - In any mode: Returns error if any reference is ambiguous
/// - Returns error if nesting depth exceeds limits
pub fn resolve_references_with_limits(
    doc: &Document,
    mode: ReferenceMode,
    limits: &Limits,
) -> HedlResult<()> {
    // Build type registries from document
    let mut registries = TypeRegistry::new();
    collect_node_ids(&doc.root, &mut registries, 0, limits)?;

    // Validate all references
    validate_references(&doc.root, &registries, mode, None, 0, limits.max_nest_depth)
}

fn collect_node_ids(
    items: &BTreeMap<String, Item>,
    registries: &mut TypeRegistry,
    depth: usize,
    limits: &Limits,
) -> HedlResult<()> {
    check_nest_depth(depth, limits.max_nest_depth)?;

    for item in items.values() {
        match item {
            Item::List(list) => {
                collect_list_ids(list, registries, depth, limits)?;
            }
            Item::Object(obj) => {
                collect_node_ids(obj, registries, depth + 1, limits)?;
            }
            Item::Scalar(_) => {}
        }
    }
    Ok(())
}

fn collect_list_ids(
    list: &MatrixList,
    registries: &mut TypeRegistry,
    depth: usize,
    limits: &Limits,
) -> HedlResult<()> {
    // Collect IDs from this list
    for node in &list.rows {
        // Node IDs were already validated during parsing, just collect them
        registries.register(&list.type_name, &node.id, 0, limits)?; // line 0 = already parsed
    }

    // Then recurse into children
    for node in &list.rows {
        if let Some(children) = node.children() {
            for child_list in children.values() {
                for child in child_list {
                    collect_list_ids_from_node(child, registries, depth + 1, limits)?;
                }
            }
        }
    }

    Ok(())
}

fn collect_list_ids_from_node(
    node: &Node,
    registries: &mut TypeRegistry,
    depth: usize,
    limits: &Limits,
) -> HedlResult<()> {
    check_nest_depth(depth, limits.max_nest_depth)?;

    registries.register(&node.type_name, &node.id, 0, limits)?;

    if let Some(children) = node.children() {
        for child_list in children.values() {
            for child in child_list {
                collect_list_ids_from_node(child, registries, depth + 1, limits)?;
            }
        }
    }

    Ok(())
}

fn validate_references(
    items: &BTreeMap<String, Item>,
    registries: &TypeRegistry,
    mode: ReferenceMode,
    current_type: Option<&str>,
    depth: usize,
    max_depth: usize,
) -> HedlResult<()> {
    check_nest_depth(depth, max_depth)?;

    for item in items.values() {
        match item {
            Item::Scalar(value) => {
                validate_value_reference(value, registries, mode, current_type)?;
            }
            Item::List(list) => {
                for node in &list.rows {
                    validate_node_references(node, registries, mode, depth, max_depth)?;
                }
            }
            Item::Object(obj) => {
                validate_references(obj, registries, mode, current_type, depth + 1, max_depth)?;
            }
        }
    }
    Ok(())
}

fn validate_node_references(
    node: &Node,
    registries: &TypeRegistry,
    mode: ReferenceMode,
    depth: usize,
    max_depth: usize,
) -> HedlResult<()> {
    check_nest_depth(depth, max_depth)?;

    for value in &node.fields {
        validate_value_reference(value, registries, mode, Some(&node.type_name))?;
    }

    if let Some(children) = node.children() {
        for child_list in children.values() {
            for child in child_list {
                validate_node_references(child, registries, mode, depth + 1, max_depth)?;
            }
        }
    }

    Ok(())
}

fn validate_value_reference(
    value: &Value,
    registries: &TypeRegistry,
    mode: ReferenceMode,
    current_type: Option<&str>,
) -> HedlResult<()> {
    if let Value::Reference(ref_val) = value {
        // If reference has explicit type (@User:u1), look only in that type's registry
        let resolved = match &ref_val.type_name {
            Some(t) => registries.contains_in_type(t, &ref_val.id),
            None => {
                // No type qualifier - behavior depends on context
                match current_type {
                    // SPEC 10.2, 10.3: In matrix context, search ONLY current type
                    Some(type_name) => registries.contains_in_type(type_name, &ref_val.id),
                    // SPEC 10.3.1: In Key-Value context, search all types but detect ambiguity
                    // P0 OPTIMIZATION: Use inverted index for O(1) lookup instead of O(m) scan
                    None => {
                        let matching_types =
                            registries.lookup_unqualified(&ref_val.id).unwrap_or(&[]);

                        match matching_types.len() {
                            0 => false, // Not found
                            1 => true,  // Unambiguous match
                            _ => {
                                // Multiple matches - ambiguous reference
                                return Err(HedlError::reference(
                                    format!(
                                        "Ambiguous unqualified reference '@{}' matches multiple types: [{}]",
                                        ref_val.id,
                                        matching_types.join(", ")
                                    ),
                                    0, // Line number lost at this point
                                ));
                            }
                        }
                    }
                }
            }
        };

        if !resolved && mode.is_strict() {
            return Err(HedlError::reference(
                format!("unresolved reference {}", ref_val.to_ref_string()),
                0, // Line number lost at this point
            ));
        }
    }

    Ok(())
}

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

    // ==================== max_total_ids limit tests ====================

    #[test]
    fn test_max_total_ids_limit() {
        let mut registry = TypeRegistry::new();
        let limits = Limits {
            max_total_ids: 3,
            ..Default::default()
        };

        // Register 3 IDs across different types (should succeed)
        assert!(registry.register("Type1", "id1", 1, &limits).is_ok());
        assert!(registry.register("Type2", "id2", 2, &limits).is_ok());
        assert!(registry.register("Type3", "id3", 3, &limits).is_ok());

        // 4th registration should fail
        let result = registry.register("Type4", "id4", 4, &limits);
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("exceeds limit"),
            "Expected 'exceeds limit' in error message, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_max_total_ids_across_types() {
        let mut registry = TypeRegistry::new();
        let limits = Limits {
            max_total_ids: 10,
            ..Default::default()
        };

        // Register IDs in same type
        for i in 0..5 {
            assert!(registry
                .register("Type1", &format!("id{}", i), i, &limits)
                .is_ok());
        }

        // Register IDs in different type
        for i in 0..5 {
            assert!(registry
                .register("Type2", &format!("id{}", i), i + 5, &limits)
                .is_ok());
        }

        // 11th registration should fail
        let result = registry.register("Type3", "id_extra", 10, &limits);
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("exceeds limit"),
            "Expected 'exceeds limit' in error message, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_unlimited_ids() {
        let mut registry = TypeRegistry::new();
        let limits = Limits::unlimited();

        // Should be able to register many IDs
        for i in 0..10000 {
            let result =
                registry.register(&format!("Type{}", i % 100), &format!("id{}", i), i, &limits);
            assert!(
                result.is_ok(),
                "Failed to register ID {} in unlimited mode",
                i
            );
        }
    }

    #[test]
    fn test_collision_detection_with_limits() {
        let mut registry = TypeRegistry::new();
        let limits = Limits::default();

        assert!(registry.register("Type1", "id1", 1, &limits).is_ok());

        // Duplicate in same type should fail with collision error, not limit error
        let result = registry.register("Type1", "id1", 2, &limits);
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("duplicate"),
            "Expected 'duplicate' in error message, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_max_total_ids_exact_limit() {
        let mut registry = TypeRegistry::new();
        let limits = Limits {
            max_total_ids: 5,
            ..Default::default()
        };

        // Register exactly at limit (should succeed)
        for i in 0..5 {
            let result = registry.register("Type", &format!("id{}", i), i, &limits);
            assert!(result.is_ok(), "Failed to register ID {} at exact limit", i);
        }

        // One more should fail
        let result = registry.register("Type", "id5", 5, &limits);
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("exceeds limit"),
            "Expected 'exceeds limit' in error message, got: {}",
            err_msg
        );
    }

    #[test]
    fn test_max_total_ids_just_under_limit() {
        let mut registry = TypeRegistry::new();
        let limits = Limits {
            max_total_ids: 5,
            ..Default::default()
        };

        // Register just under limit (should succeed)
        for i in 0..4 {
            assert!(registry
                .register("Type", &format!("id{}", i), i, &limits)
                .is_ok());
        }

        // Still have room for one more
        assert!(registry.register("Type", "id4", 4, &limits).is_ok());
    }

    #[test]
    fn test_max_total_ids_error_message_clarity() {
        let mut registry = TypeRegistry::new();
        let limits = Limits {
            max_total_ids: 2,
            ..Default::default()
        };

        registry.register("Type1", "id1", 1, &limits).unwrap();
        registry.register("Type2", "id2", 2, &limits).unwrap();

        let result = registry.register("Type3", "id3", 3, &limits);
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("2"),
            "Error message should contain the count"
        );
        assert!(
            err_msg.contains("limit"),
            "Error message should mention 'limit'"
        );
    }

    #[test]
    fn test_total_ids_count_tracking() {
        let mut registry = TypeRegistry::new();
        let limits = Limits::unlimited();

        assert_eq!(registry.total_ids, 0);

        registry.register("Type1", "id1", 1, &limits).unwrap();
        assert_eq!(registry.total_ids, 1);

        registry.register("Type2", "id2", 2, &limits).unwrap();
        assert_eq!(registry.total_ids, 2);

        registry.register("Type1", "id3", 3, &limits).unwrap();
        assert_eq!(registry.total_ids, 3);
    }

    #[test]
    fn test_max_total_ids_with_multiple_types() {
        let mut registry = TypeRegistry::new();
        let limits = Limits {
            max_total_ids: 100,
            ..Default::default()
        };

        // Distribute IDs across 10 types
        for type_idx in 0..10 {
            for id_idx in 0..10 {
                let result = registry.register(
                    &format!("Type{}", type_idx),
                    &format!("id{}_{}", type_idx, id_idx),
                    type_idx * 10 + id_idx,
                    &limits,
                );
                assert!(result.is_ok(), "Failed at type {} id {}", type_idx, id_idx);
            }
        }

        // Now we're at limit (100), next should fail
        let result = registry.register("TypeExtra", "extra", 100, &limits);
        assert!(result.is_err());
    }

    #[test]
    fn test_collision_preserves_total_count() {
        let mut registry = TypeRegistry::new();
        let limits = Limits::unlimited();

        registry.register("Type1", "id1", 1, &limits).unwrap();
        assert_eq!(registry.total_ids, 1);

        // Attempt duplicate registration (should fail)
        let result = registry.register("Type1", "id1", 2, &limits);
        assert!(result.is_err());

        // Total count should not have changed
        assert_eq!(registry.total_ids, 1);
    }

    #[test]
    fn test_default_limits_max_total_ids() {
        let limits = Limits::default();
        assert_eq!(limits.max_total_ids, 10_000_000);
    }

    #[test]
    fn test_unlimited_limits_max_total_ids() {
        let limits = Limits::unlimited();
        assert_eq!(limits.max_total_ids, usize::MAX);
    }

    // ==================== TypeRegistry basic functionality tests ====================

    #[test]
    fn test_registry_new() {
        let registry = TypeRegistry::new();
        assert_eq!(registry.total_ids, 0);
        assert!(registry.by_type.is_empty());
        assert!(registry.by_id.is_empty());
    }

    #[test]
    fn test_registry_default() {
        let registry = TypeRegistry::default();
        assert_eq!(registry.total_ids, 0);
    }

    #[test]
    fn test_contains_in_type() {
        let mut registry = TypeRegistry::new();
        let limits = Limits::unlimited();

        registry.register("User", "u1", 1, &limits).unwrap();
        assert!(registry.contains_in_type("User", "u1"));
        assert!(!registry.contains_in_type("User", "u2"));
        assert!(!registry.contains_in_type("Post", "u1"));
    }

    #[test]
    fn test_lookup_unqualified() {
        let mut registry = TypeRegistry::new();
        let limits = Limits::unlimited();

        registry.register("User", "id1", 1, &limits).unwrap();
        registry.register("Post", "id1", 2, &limits).unwrap();

        let types = registry.lookup_unqualified("id1");
        assert!(types.is_some());
        let types = types.unwrap();
        assert_eq!(types.len(), 2);
        assert!(types.contains(&"User".to_string()));
        assert!(types.contains(&"Post".to_string()));

        let not_found = registry.lookup_unqualified("nonexistent");
        assert!(not_found.is_none());
    }

    #[test]
    fn test_inverted_index_maintenance() {
        let mut registry = TypeRegistry::new();
        let limits = Limits::unlimited();

        // Same ID in multiple types should appear in inverted index
        registry.register("Type1", "shared_id", 1, &limits).unwrap();
        registry.register("Type2", "shared_id", 2, &limits).unwrap();
        registry.register("Type3", "shared_id", 3, &limits).unwrap();

        let types = registry.lookup_unqualified("shared_id").unwrap();
        assert_eq!(types.len(), 3);
    }

    // ==================== ReferenceMode tests ====================

    #[test]
    fn test_reference_mode_default() {
        assert_eq!(ReferenceMode::default(), ReferenceMode::Strict);
    }

    #[test]
    fn test_reference_mode_from_bool() {
        assert_eq!(ReferenceMode::from(true), ReferenceMode::Strict);
        assert_eq!(ReferenceMode::from(false), ReferenceMode::Lenient);
    }

    #[test]
    fn test_reference_mode_is_strict() {
        assert!(ReferenceMode::Strict.is_strict());
        assert!(!ReferenceMode::Lenient.is_strict());
    }

    #[test]
    fn test_reference_mode_is_lenient() {
        assert!(ReferenceMode::Lenient.is_lenient());
        assert!(!ReferenceMode::Strict.is_lenient());
    }

    #[test]
    fn test_reference_mode_equality() {
        assert_eq!(ReferenceMode::Strict, ReferenceMode::Strict);
        assert_eq!(ReferenceMode::Lenient, ReferenceMode::Lenient);
        assert_ne!(ReferenceMode::Strict, ReferenceMode::Lenient);
    }
}