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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
// 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.

//! Schema versioning for HEDL types.
//!
//! This module provides version tracking for schemas, enabling schema evolution
//! and compatibility checking across different versions of HEDL documents.
//!
//! # Schema Evolution
//!
//! Schema versioning allows HEDL documents to specify which version of a schema
//! they conform to. This enables:
//!
//! - Forward compatibility: Older parsers can recognize newer schemas
//! - Backward compatibility: Newer parsers can read older documents
//! - Migration tooling: Automated schema migrations between versions
//!
//! # Version Format
//!
//! Versions follow semantic versioning: `major.minor.patch`
//!
//! - `major`: Breaking changes (field removals, type changes)
//! - `minor`: Backward-compatible additions (new optional fields)
//! - `patch`: Bug fixes to schema definitions

use std::fmt;

/// Schema version identifier.
///
/// Represents the version of a type's schema using semantic versioning.
///
/// # Examples
///
/// ```
/// use hedl_core::schema_version::SchemaVersion;
///
/// // Create a version
/// let v1 = SchemaVersion::new(1, 0, 0);
/// let v2 = SchemaVersion::new(1, 1, 0);
///
/// // Compare versions
/// assert!(v2 > v1);
/// assert!(v2.is_compatible_with(&v1));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct SchemaVersion {
    /// Major version (breaking changes)
    pub major: u32,
    /// Minor version (backward-compatible additions)
    pub minor: u32,
    /// Patch version (bug fixes)
    pub patch: u32,
}

impl SchemaVersion {
    /// Create a new schema version.
    ///
    /// # Arguments
    ///
    /// * `major` - Major version number
    /// * `minor` - Minor version number
    /// * `patch` - Patch version number
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::schema_version::SchemaVersion;
    ///
    /// let version = SchemaVersion::new(1, 2, 3);
    /// assert_eq!(version.major, 1);
    /// assert_eq!(version.minor, 2);
    /// assert_eq!(version.patch, 3);
    /// ```
    pub const fn new(major: u32, minor: u32, patch: u32) -> Self {
        Self {
            major,
            minor,
            patch,
        }
    }

    /// Create version 1.0.0 (common default).
    pub const fn v1() -> Self {
        Self::new(1, 0, 0)
    }

    /// Check if this version is compatible with another version.
    ///
    /// Compatibility rules:
    /// - Same major version required
    /// - This version's minor must be >= other's minor
    ///
    /// # Arguments
    ///
    /// * `other` - The version to check compatibility with
    ///
    /// # Returns
    ///
    /// `true` if this schema can read data written for `other`
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::schema_version::SchemaVersion;
    ///
    /// let v1_0 = SchemaVersion::new(1, 0, 0);
    /// let v1_1 = SchemaVersion::new(1, 1, 0);
    /// let v2_0 = SchemaVersion::new(2, 0, 0);
    ///
    /// // v1.1 can read v1.0 data (backward compatible)
    /// assert!(v1_1.is_compatible_with(&v1_0));
    ///
    /// // v1.0 cannot read v1.1 data (missing new fields)
    /// assert!(!v1_0.is_compatible_with(&v1_1));
    ///
    /// // Different major versions are not compatible
    /// assert!(!v2_0.is_compatible_with(&v1_0));
    /// ```
    pub fn is_compatible_with(&self, other: &Self) -> bool {
        // Same major version required
        if self.major != other.major {
            return false;
        }

        // This version must be >= other's minor version
        // (can read data with same or fewer features)
        self.minor >= other.minor
    }

    /// Check if this is a breaking change from another version.
    ///
    /// A breaking change occurs when the major version increases.
    pub fn is_breaking_from(&self, other: &Self) -> bool {
        self.major != other.major
    }

    /// Parse a version string in "major.minor.patch" format.
    ///
    /// # Arguments
    ///
    /// * `s` - Version string to parse
    ///
    /// # Returns
    ///
    /// `Some(SchemaVersion)` if parsing succeeds, `None` otherwise
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::schema_version::SchemaVersion;
    ///
    /// let v = SchemaVersion::parse("1.2.3").unwrap();
    /// assert_eq!(v, SchemaVersion::new(1, 2, 3));
    ///
    /// // Short forms also work
    /// let v = SchemaVersion::parse("1.2").unwrap();
    /// assert_eq!(v, SchemaVersion::new(1, 2, 0));
    ///
    /// let v = SchemaVersion::parse("1").unwrap();
    /// assert_eq!(v, SchemaVersion::new(1, 0, 0));
    /// ```
    pub fn parse(s: &str) -> Option<Self> {
        let parts: Vec<&str> = s.trim().split('.').collect();

        if parts.is_empty() || parts.len() > 3 {
            return None;
        }

        let major = parts.first()?.parse().ok()?;
        let minor = parts.get(1).and_then(|s| s.parse().ok()).unwrap_or(0);
        let patch = parts.get(2).and_then(|s| s.parse().ok()).unwrap_or(0);

        Some(Self::new(major, minor, patch))
    }
}

impl Default for SchemaVersion {
    fn default() -> Self {
        Self::v1()
    }
}

impl fmt::Display for SchemaVersion {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}.{}.{}", self.major, self.minor, self.patch)
    }
}

/// Parse a schema version from a string.
impl std::str::FromStr for SchemaVersion {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s).ok_or("invalid version format")
    }
}

/// Field definition with optional default value.
///
/// Represents a field in a schema with its name, whether it's optional,
/// and an optional default value.
///
/// # Examples
///
/// ```
/// use hedl_core::schema_version::FieldDef;
/// use hedl_core::Value;
///
/// // Required field
/// let id_field = FieldDef {
///     name: "id".to_string(),
///     optional: false,
///     default: None,
/// };
///
/// // Optional field with default
/// let status_field = FieldDef {
///     name: "status".to_string(),
///     optional: true,
///     default: Some(Value::String("active".to_string().into())),
/// };
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct FieldDef {
    /// The field name.
    pub name: String,
    /// Whether the field is optional.
    pub optional: bool,
    /// Default value if the field is omitted.
    pub default: Option<crate::Value>,
}

impl FieldDef {
    /// Create a new required field definition.
    ///
    /// # Arguments
    ///
    /// * `name` - The field name
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::schema_version::FieldDef;
    ///
    /// let field = FieldDef::required("id");
    /// assert!(!field.optional);
    /// assert!(field.default.is_none());
    /// ```
    pub fn required(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            optional: false,
            default: None,
        }
    }

    /// Create a new optional field definition.
    ///
    /// # Arguments
    ///
    /// * `name` - The field name
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::schema_version::FieldDef;
    ///
    /// let field = FieldDef::optional("description");
    /// assert!(field.optional);
    /// assert!(field.default.is_none());
    /// ```
    pub fn optional(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            optional: true,
            default: None,
        }
    }

    /// Create a new optional field with a default value.
    ///
    /// # Arguments
    ///
    /// * `name` - The field name
    /// * `default` - The default value
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::schema_version::FieldDef;
    /// use hedl_core::Value;
    ///
    /// let field = FieldDef::with_default("active", Value::Bool(true));
    /// assert!(field.optional);
    /// assert_eq!(field.default, Some(Value::Bool(true)));
    /// ```
    pub fn with_default(name: impl Into<String>, default: crate::Value) -> Self {
        Self {
            name: name.into(),
            optional: true,
            default: Some(default),
        }
    }
}

/// Schema definition with versioning.
///
/// Represents a complete schema for a type, including version information
/// and field definitions.
///
/// # Examples
///
/// ```
/// use hedl_core::schema_version::{Schema, SchemaVersion, FieldDef};
///
/// let mut schema = Schema::new(SchemaVersion::new(1, 0, 0));
/// schema.add_type("User", vec![
///     FieldDef::required("id"),
///     FieldDef::required("name"),
///     FieldDef::optional("email"),
/// ]);
/// ```
#[derive(Debug, Clone)]
pub struct Schema {
    /// Schema version.
    pub version: SchemaVersion,
    /// Type definitions: type name -> field definitions.
    pub types: std::collections::BTreeMap<String, Vec<FieldDef>>,
}

impl Schema {
    /// Create a new schema with the specified version.
    ///
    /// # Arguments
    ///
    /// * `version` - The schema version
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::schema_version::{Schema, SchemaVersion};
    ///
    /// let schema = Schema::new(SchemaVersion::new(1, 0, 0));
    /// assert_eq!(schema.version, SchemaVersion::new(1, 0, 0));
    /// assert!(schema.types.is_empty());
    /// ```
    pub fn new(version: SchemaVersion) -> Self {
        Self {
            version,
            types: std::collections::BTreeMap::new(),
        }
    }

    /// Add a type definition to the schema.
    ///
    /// # Arguments
    ///
    /// * `name` - The type name
    /// * `fields` - Field definitions for the type
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::schema_version::{Schema, SchemaVersion, FieldDef};
    ///
    /// let mut schema = Schema::new(SchemaVersion::new(1, 0, 0));
    /// schema.add_type("User", vec![
    ///     FieldDef::required("id"),
    ///     FieldDef::required("name"),
    /// ]);
    /// assert!(schema.types.contains_key("User"));
    /// ```
    pub fn add_type(&mut self, name: &str, fields: Vec<FieldDef>) {
        self.types.insert(name.to_string(), fields);
    }

    /// Get field definitions for a type.
    ///
    /// # Arguments
    ///
    /// * `type_name` - The type name to look up
    ///
    /// # Returns
    ///
    /// `Some(&Vec<FieldDef>)` if the type exists, `None` otherwise
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::schema_version::{Schema, SchemaVersion, FieldDef};
    ///
    /// let mut schema = Schema::new(SchemaVersion::new(1, 0, 0));
    /// schema.add_type("User", vec![FieldDef::required("id")]);
    ///
    /// assert!(schema.get_fields("User").is_some());
    /// assert!(schema.get_fields("Post").is_none());
    /// ```
    pub fn get_fields(&self, type_name: &str) -> Option<&Vec<FieldDef>> {
        self.types.get(type_name)
    }

    /// Check if this schema is compatible with another schema.
    ///
    /// Schemas are compatible if their versions are compatible.
    ///
    /// # Arguments
    ///
    /// * `other` - The schema to check compatibility with
    ///
    /// # Returns
    ///
    /// `true` if this schema can read data written for `other`
    ///
    /// # Examples
    ///
    /// ```
    /// use hedl_core::schema_version::{Schema, SchemaVersion};
    ///
    /// let v1 = Schema::new(SchemaVersion::new(1, 0, 0));
    /// let v1_1 = Schema::new(SchemaVersion::new(1, 1, 0));
    /// let v2 = Schema::new(SchemaVersion::new(2, 0, 0));
    ///
    /// assert!(v1_1.is_compatible_with(&v1));
    /// assert!(!v1.is_compatible_with(&v1_1));
    /// assert!(!v2.is_compatible_with(&v1));
    /// ```
    pub fn is_compatible_with(&self, other: &Self) -> bool {
        self.version.is_compatible_with(&other.version)
    }
}

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

    // ==================== Construction tests ====================

    #[test]
    fn test_new() {
        let v = SchemaVersion::new(1, 2, 3);
        assert_eq!(v.major, 1);
        assert_eq!(v.minor, 2);
        assert_eq!(v.patch, 3);
    }

    #[test]
    fn test_v1() {
        let v = SchemaVersion::v1();
        assert_eq!(v, SchemaVersion::new(1, 0, 0));
    }

    #[test]
    fn test_default() {
        let v = SchemaVersion::default();
        assert_eq!(v, SchemaVersion::v1());
    }

    // ==================== Parsing tests ====================

    #[test]
    fn test_parse_full() {
        let v = SchemaVersion::parse("1.2.3").unwrap();
        assert_eq!(v, SchemaVersion::new(1, 2, 3));
    }

    #[test]
    fn test_parse_major_minor() {
        let v = SchemaVersion::parse("1.2").unwrap();
        assert_eq!(v, SchemaVersion::new(1, 2, 0));
    }

    #[test]
    fn test_parse_major_only() {
        let v = SchemaVersion::parse("1").unwrap();
        assert_eq!(v, SchemaVersion::new(1, 0, 0));
    }

    #[test]
    fn test_parse_with_whitespace() {
        let v = SchemaVersion::parse("  1.2.3  ").unwrap();
        assert_eq!(v, SchemaVersion::new(1, 2, 3));
    }

    #[test]
    fn test_parse_invalid_empty() {
        assert!(SchemaVersion::parse("").is_none());
    }

    #[test]
    fn test_parse_invalid_non_numeric() {
        assert!(SchemaVersion::parse("a.b.c").is_none());
    }

    #[test]
    fn test_parse_invalid_too_many_parts() {
        assert!(SchemaVersion::parse("1.2.3.4").is_none());
    }

    #[test]
    fn test_from_str() {
        let v: SchemaVersion = "1.2.3".parse().unwrap();
        assert_eq!(v, SchemaVersion::new(1, 2, 3));
    }

    #[test]
    fn test_from_str_invalid() {
        let result: Result<SchemaVersion, _> = "invalid".parse();
        assert!(result.is_err());
    }

    // ==================== Display tests ====================

    #[test]
    fn test_display() {
        let v = SchemaVersion::new(1, 2, 3);
        assert_eq!(v.to_string(), "1.2.3");
    }

    #[test]
    fn test_display_zeros() {
        let v = SchemaVersion::new(1, 0, 0);
        assert_eq!(v.to_string(), "1.0.0");
    }

    // ==================== Comparison tests ====================

    #[test]
    fn test_equality() {
        let v1 = SchemaVersion::new(1, 2, 3);
        let v2 = SchemaVersion::new(1, 2, 3);
        assert_eq!(v1, v2);
    }

    #[test]
    fn test_inequality() {
        let v1 = SchemaVersion::new(1, 2, 3);
        let v2 = SchemaVersion::new(1, 2, 4);
        assert_ne!(v1, v2);
    }

    #[test]
    fn test_ordering_major() {
        let v1 = SchemaVersion::new(1, 0, 0);
        let v2 = SchemaVersion::new(2, 0, 0);
        assert!(v2 > v1);
    }

    #[test]
    fn test_ordering_minor() {
        let v1 = SchemaVersion::new(1, 1, 0);
        let v2 = SchemaVersion::new(1, 2, 0);
        assert!(v2 > v1);
    }

    #[test]
    fn test_ordering_patch() {
        let v1 = SchemaVersion::new(1, 0, 1);
        let v2 = SchemaVersion::new(1, 0, 2);
        assert!(v2 > v1);
    }

    // ==================== Compatibility tests ====================

    #[test]
    fn test_compatible_same_version() {
        let v = SchemaVersion::new(1, 2, 3);
        assert!(v.is_compatible_with(&v));
    }

    #[test]
    fn test_compatible_higher_minor() {
        let v1_0 = SchemaVersion::new(1, 0, 0);
        let v1_1 = SchemaVersion::new(1, 1, 0);
        // v1.1 can read v1.0 data
        assert!(v1_1.is_compatible_with(&v1_0));
    }

    #[test]
    fn test_incompatible_lower_minor() {
        let v1_0 = SchemaVersion::new(1, 0, 0);
        let v1_1 = SchemaVersion::new(1, 1, 0);
        // v1.0 cannot read v1.1 data
        assert!(!v1_0.is_compatible_with(&v1_1));
    }

    #[test]
    fn test_incompatible_different_major() {
        let v1 = SchemaVersion::new(1, 0, 0);
        let v2 = SchemaVersion::new(2, 0, 0);
        assert!(!v2.is_compatible_with(&v1));
        assert!(!v1.is_compatible_with(&v2));
    }

    #[test]
    fn test_compatible_different_patch() {
        let v1 = SchemaVersion::new(1, 0, 0);
        let v2 = SchemaVersion::new(1, 0, 5);
        // Patch versions don't affect compatibility
        assert!(v1.is_compatible_with(&v2));
        assert!(v2.is_compatible_with(&v1));
    }

    // ==================== Breaking change tests ====================

    #[test]
    fn test_breaking_change_true() {
        let v1 = SchemaVersion::new(1, 0, 0);
        let v2 = SchemaVersion::new(2, 0, 0);
        assert!(v2.is_breaking_from(&v1));
    }

    #[test]
    fn test_breaking_change_false_same_major() {
        let v1 = SchemaVersion::new(1, 0, 0);
        let v2 = SchemaVersion::new(1, 5, 0);
        assert!(!v2.is_breaking_from(&v1));
    }

    #[test]
    fn test_breaking_change_false_same_version() {
        let v = SchemaVersion::new(1, 2, 3);
        assert!(!v.is_breaking_from(&v));
    }

    // ==================== Hash tests ====================

    #[test]
    fn test_hash_consistency() {
        use std::collections::HashSet;

        let mut set = HashSet::new();
        set.insert(SchemaVersion::new(1, 0, 0));
        set.insert(SchemaVersion::new(1, 0, 0)); // duplicate

        assert_eq!(set.len(), 1);
    }

    #[test]
    fn test_hash_different_versions() {
        use std::collections::HashSet;

        let mut set = HashSet::new();
        set.insert(SchemaVersion::new(1, 0, 0));
        set.insert(SchemaVersion::new(1, 1, 0));
        set.insert(SchemaVersion::new(2, 0, 0));

        assert_eq!(set.len(), 3);
    }

    // ==================== Clone and Copy tests ====================

    #[test]
    fn test_clone() {
        let v1 = SchemaVersion::new(1, 2, 3);
        let v2 = v1;
        assert_eq!(v1, v2);
    }

    #[test]
    fn test_copy() {
        let v1 = SchemaVersion::new(1, 2, 3);
        let v2 = v1; // Copy, not move
        assert_eq!(v1, v2);
    }

    // ==================== Debug tests ====================

    #[test]
    fn test_debug() {
        let v = SchemaVersion::new(1, 2, 3);
        let debug = format!("{:?}", v);
        assert!(debug.contains("SchemaVersion"));
        assert!(debug.contains("major: 1"));
        assert!(debug.contains("minor: 2"));
        assert!(debug.contains("patch: 3"));
    }

    // ==================== Edge cases ====================

    #[test]
    fn test_max_values() {
        let v = SchemaVersion::new(u32::MAX, u32::MAX, u32::MAX);
        assert_eq!(v.major, u32::MAX);
        assert_eq!(v.minor, u32::MAX);
        assert_eq!(v.patch, u32::MAX);
    }

    #[test]
    fn test_zero_version() {
        let v = SchemaVersion::new(0, 0, 0);
        assert_eq!(v.to_string(), "0.0.0");
    }

    // ==================== FieldDef tests ====================

    #[test]
    fn test_field_def_required() {
        let field = FieldDef::required("id");
        assert_eq!(field.name, "id");
        assert!(!field.optional);
        assert!(field.default.is_none());
    }

    #[test]
    fn test_field_def_optional() {
        let field = FieldDef::optional("description");
        assert_eq!(field.name, "description");
        assert!(field.optional);
        assert!(field.default.is_none());
    }

    #[test]
    fn test_field_def_with_default() {
        use crate::Value;
        let field = FieldDef::with_default("active", Value::Bool(true));
        assert_eq!(field.name, "active");
        assert!(field.optional);
        assert_eq!(field.default, Some(Value::Bool(true)));
    }

    #[test]
    fn test_field_def_with_default_string() {
        use crate::Value;
        let field = FieldDef::with_default("status", Value::String("pending".to_string().into()));
        assert_eq!(field.name, "status");
        assert!(field.optional);
        assert_eq!(
            field.default,
            Some(Value::String("pending".to_string().into()))
        );
    }

    #[test]
    fn test_field_def_with_default_int() {
        use crate::Value;
        let field = FieldDef::with_default("count", Value::Int(0));
        assert_eq!(field.name, "count");
        assert_eq!(field.default, Some(Value::Int(0)));
    }

    #[test]
    fn test_field_def_equality() {
        let a = FieldDef::required("id");
        let b = FieldDef::required("id");
        assert_eq!(a, b);
    }

    #[test]
    fn test_field_def_inequality() {
        let a = FieldDef::required("id");
        let b = FieldDef::optional("id");
        assert_ne!(a, b);
    }

    #[test]
    fn test_field_def_clone() {
        use crate::Value;
        let original = FieldDef::with_default("test", Value::Int(42));
        let cloned = original.clone();
        assert_eq!(original, cloned);
    }

    #[test]
    fn test_field_def_debug() {
        let field = FieldDef::required("id");
        let debug = format!("{:?}", field);
        assert!(debug.contains("FieldDef"));
        assert!(debug.contains("id"));
    }

    // ==================== Schema tests ====================

    #[test]
    fn test_schema_new() {
        let schema = Schema::new(SchemaVersion::new(1, 0, 0));
        assert_eq!(schema.version, SchemaVersion::new(1, 0, 0));
        assert!(schema.types.is_empty());
    }

    #[test]
    fn test_schema_add_type() {
        let mut schema = Schema::new(SchemaVersion::new(1, 0, 0));
        schema.add_type(
            "User",
            vec![FieldDef::required("id"), FieldDef::required("name")],
        );
        assert!(schema.types.contains_key("User"));
        assert_eq!(schema.types["User"].len(), 2);
    }

    #[test]
    fn test_schema_add_multiple_types() {
        let mut schema = Schema::new(SchemaVersion::new(1, 0, 0));
        schema.add_type("User", vec![FieldDef::required("id")]);
        schema.add_type("Post", vec![FieldDef::required("id")]);
        assert_eq!(schema.types.len(), 2);
        assert!(schema.types.contains_key("User"));
        assert!(schema.types.contains_key("Post"));
    }

    #[test]
    fn test_schema_get_fields() {
        let mut schema = Schema::new(SchemaVersion::new(1, 0, 0));
        schema.add_type(
            "User",
            vec![FieldDef::required("id"), FieldDef::optional("email")],
        );

        let fields = schema.get_fields("User").unwrap();
        assert_eq!(fields.len(), 2);
        assert_eq!(fields[0].name, "id");
        assert_eq!(fields[1].name, "email");
    }

    #[test]
    fn test_schema_get_fields_missing() {
        let schema = Schema::new(SchemaVersion::new(1, 0, 0));
        assert!(schema.get_fields("MissingType").is_none());
    }

    #[test]
    fn test_schema_is_compatible_with() {
        let v1 = Schema::new(SchemaVersion::new(1, 0, 0));
        let v1_1 = Schema::new(SchemaVersion::new(1, 1, 0));
        let v2 = Schema::new(SchemaVersion::new(2, 0, 0));

        assert!(v1_1.is_compatible_with(&v1));
        assert!(!v1.is_compatible_with(&v1_1));
        assert!(!v2.is_compatible_with(&v1));
    }

    #[test]
    fn test_schema_replace_type() {
        let mut schema = Schema::new(SchemaVersion::new(1, 0, 0));
        schema.add_type("User", vec![FieldDef::required("id")]);
        schema.add_type(
            "User",
            vec![FieldDef::required("id"), FieldDef::required("name")],
        );

        let fields = schema.get_fields("User").unwrap();
        assert_eq!(fields.len(), 2);
    }

    #[test]
    fn test_schema_clone() {
        let mut schema = Schema::new(SchemaVersion::new(1, 0, 0));
        schema.add_type("User", vec![FieldDef::required("id")]);
        let cloned = schema.clone();
        assert_eq!(schema.version, cloned.version);
        assert_eq!(schema.types.len(), cloned.types.len());
    }

    #[test]
    fn test_schema_debug() {
        let schema = Schema::new(SchemaVersion::new(1, 0, 0));
        let debug = format!("{:?}", schema);
        assert!(debug.contains("Schema"));
        assert!(debug.contains("version"));
    }

    #[test]
    fn test_schema_with_optional_and_default_fields() {
        use crate::Value;
        let mut schema = Schema::new(SchemaVersion::new(1, 0, 0));
        schema.add_type(
            "User",
            vec![
                FieldDef::required("id"),
                FieldDef::optional("name"),
                FieldDef::with_default("active", Value::Bool(true)),
                FieldDef::with_default("role", Value::String("user".to_string().into())),
            ],
        );

        let fields = schema.get_fields("User").unwrap();
        assert_eq!(fields.len(), 4);
        assert!(!fields[0].optional); // id is required
        assert!(fields[1].optional); // name is optional
        assert!(fields[2].optional); // active has default
        assert!(fields[3].optional); // role has default
        assert!(fields[2].default.is_some());
        assert!(fields[3].default.is_some());
    }

    // ==================== Integration tests ====================

    #[test]
    fn test_schema_evolution_scenario() {
        use crate::Value;

        // Version 1.0.0: Initial schema
        let mut v1 = Schema::new(SchemaVersion::new(1, 0, 0));
        v1.add_type(
            "User",
            vec![FieldDef::required("id"), FieldDef::required("name")],
        );

        // Version 1.1.0: Add optional field (backward compatible)
        let mut v1_1 = Schema::new(SchemaVersion::new(1, 1, 0));
        v1_1.add_type(
            "User",
            vec![
                FieldDef::required("id"),
                FieldDef::required("name"),
                FieldDef::optional("email"),
            ],
        );

        // Version 1.2.0: Add field with default (backward compatible)
        let mut v1_2 = Schema::new(SchemaVersion::new(1, 2, 0));
        v1_2.add_type(
            "User",
            vec![
                FieldDef::required("id"),
                FieldDef::required("name"),
                FieldDef::optional("email"),
                FieldDef::with_default("active", Value::Bool(true)),
            ],
        );

        // Verify compatibility
        assert!(v1_1.is_compatible_with(&v1)); // v1.1 can read v1.0 data
        assert!(v1_2.is_compatible_with(&v1_1)); // v1.2 can read v1.1 data
        assert!(v1_2.is_compatible_with(&v1)); // v1.2 can read v1.0 data

        // Older versions cannot read newer data
        assert!(!v1.is_compatible_with(&v1_1));
        assert!(!v1_1.is_compatible_with(&v1_2));
    }

    #[test]
    fn test_breaking_schema_change() {
        // Version 1.0.0
        let mut v1 = Schema::new(SchemaVersion::new(1, 0, 0));
        v1.add_type("User", vec![FieldDef::required("id")]);

        // Version 2.0.0: Breaking change
        let mut v2 = Schema::new(SchemaVersion::new(2, 0, 0));
        v2.add_type(
            "User",
            vec![
                FieldDef::required("user_id"), // Changed field name
                FieldDef::required("name"),
            ],
        );

        // Breaking changes are not compatible
        assert!(!v2.is_compatible_with(&v1));
        assert!(!v1.is_compatible_with(&v2));
    }

    #[test]
    fn test_field_def_with_null_default() {
        use crate::Value;
        let field = FieldDef::with_default("optional_value", Value::Null);
        assert_eq!(field.default, Some(Value::Null));
    }

    #[test]
    fn test_empty_schema() {
        let schema = Schema::new(SchemaVersion::new(0, 0, 1));
        assert!(schema.types.is_empty());
        assert!(schema.get_fields("AnyType").is_none());
    }

    #[test]
    fn test_schema_with_empty_field_list() {
        let mut schema = Schema::new(SchemaVersion::new(1, 0, 0));
        schema.add_type("EmptyType", vec![]);
        let fields = schema.get_fields("EmptyType").unwrap();
        assert!(fields.is_empty());
    }
}