hydrate-schema 0.0.2

Game asset pipeline and authoring framework
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
use crate::{
    HashMap, HashSet, Schema, SchemaDynamicArray, SchemaEnum, SchemaEnumSymbol, SchemaFingerprint,
    SchemaMap, SchemaNamedType, SchemaRecord, SchemaRecordField, SchemaStaticArray,
};
use std::fmt::Formatter;
use std::hash::{Hash, Hasher};
use std::path::PathBuf;
use uuid::Uuid;

#[derive(Debug)]
pub enum SchemaDefValidationError {
    DuplicateFieldName(String, String),
    ReferencedNamedTypeNotFound(String, String),
    // Map keys cannot be f32/f64, containers, nullables, records, etc.
    InvalidMapKeyType(String, String),
    // AssetRef can only reference named types that are records
    InvalidAssetRefInnerType(String, String),
}

impl std::fmt::Display for SchemaDefValidationError {
    fn fmt(
        &self,
        f: &mut Formatter<'_>,
    ) -> std::fmt::Result {
        match self {
            SchemaDefValidationError::DuplicateFieldName(schema_name, duplicate_field_name) => {
                write!(
                    f,
                    "Schema {} has a duplicate field {}",
                    schema_name, duplicate_field_name
                )
            }
            SchemaDefValidationError::ReferencedNamedTypeNotFound(
                schema_name,
                referenced_named_type_not_found,
            ) => write!(
                f,
                "Schema {} references a type {} that wasn't found",
                schema_name, referenced_named_type_not_found
            ),
            SchemaDefValidationError::InvalidMapKeyType(schema_name, invalid_map_key_type) => {
                write!(
                    f,
                    "Schema {} has map with key of type {}, but this type cannot be used as a key",
                    schema_name, invalid_map_key_type
                )
            }
            SchemaDefValidationError::InvalidAssetRefInnerType(
                schema_name,
                invalid_asset_ref_inner_type,
            ) => write!(
                f,
                "Schema {} references an AssetRef that references {} but it is not a record",
                schema_name, invalid_asset_ref_inner_type
            ),
        }
    }
}

pub type SchemaDefValidationResult<T> = Result<T, SchemaDefValidationError>;

#[derive(Debug)]
pub enum SchemaDefParserError {
    Str(&'static str),
    String(String),
    ValidationError(SchemaDefValidationError),
}

impl From<SchemaDefValidationError> for SchemaDefParserError {
    fn from(validation_error: SchemaDefValidationError) -> Self {
        SchemaDefParserError::ValidationError(validation_error)
    }
}

pub type SchemaDefParserResult<T> = Result<T, SchemaDefParserError>;

#[derive(Debug)]
pub struct SchemaDefStaticArray {
    pub(super) item_type: Box<SchemaDefType>,
    pub(super) length: usize,
}

impl SchemaDefStaticArray {
    fn apply_type_aliases(
        &mut self,
        aliases: &HashMap<String, String>,
    ) {
        self.item_type.apply_type_aliases(aliases);
    }

    fn collect_all_related_types(
        &self,
        types: &mut HashSet<String>,
    ) {
        self.item_type.collect_all_related_types(types);
    }

    fn partial_hash<T: Hasher>(
        &self,
        hasher: &mut T,
    ) {
        self.item_type.partial_hash(hasher);
        self.length.hash(hasher);
    }

    fn to_schema(
        &self,
        named_types: &HashMap<String, SchemaDefNamedType>,
        fingerprints: &HashMap<String, SchemaFingerprint>,
    ) -> SchemaStaticArray {
        SchemaStaticArray::new(
            Box::new(self.item_type.to_schema(named_types, fingerprints)),
            self.length,
        )
    }
}

#[derive(Debug)]
pub struct SchemaDefDynamicArray {
    pub(super) item_type: Box<SchemaDefType>,
}

impl SchemaDefDynamicArray {
    pub fn new(item_type: Box<SchemaDefType>) -> Self {
        SchemaDefDynamicArray { item_type }
    }

    fn apply_type_aliases(
        &mut self,
        aliases: &HashMap<String, String>,
    ) {
        self.item_type.apply_type_aliases(aliases);
    }

    fn collect_all_related_types(
        &self,
        types: &mut HashSet<String>,
    ) {
        self.item_type.collect_all_related_types(types);
    }

    fn partial_hash<T: Hasher>(
        &self,
        hasher: &mut T,
    ) {
        self.item_type.partial_hash(hasher);
    }

    fn to_schema(
        &self,
        named_types: &HashMap<String, SchemaDefNamedType>,
        fingerprints: &HashMap<String, SchemaFingerprint>,
    ) -> SchemaDynamicArray {
        SchemaDynamicArray::new(Box::new(
            self.item_type.to_schema(named_types, fingerprints),
        ))
    }
}

#[derive(Debug)]
pub struct SchemaDefMap {
    pub(super) key_type: Box<SchemaDefType>,
    pub(super) value_type: Box<SchemaDefType>,
}

impl SchemaDefMap {
    fn apply_type_aliases(
        &mut self,
        aliases: &HashMap<String, String>,
    ) {
        self.key_type.apply_type_aliases(aliases);
        self.value_type.apply_type_aliases(aliases);
    }

    fn collect_all_related_types(
        &self,
        types: &mut HashSet<String>,
    ) {
        self.key_type.collect_all_related_types(types);
        self.value_type.collect_all_related_types(types);
    }

    fn partial_hash<T: Hasher>(
        &self,
        hasher: &mut T,
    ) {
        self.key_type.partial_hash(hasher);
        self.value_type.partial_hash(hasher);
    }

    fn to_schema(
        &self,
        named_types: &HashMap<String, SchemaDefNamedType>,
        fingerprints: &HashMap<String, SchemaFingerprint>,
    ) -> SchemaMap {
        SchemaMap::new(
            Box::new(self.key_type.to_schema(named_types, fingerprints)),
            Box::new(self.value_type.to_schema(named_types, fingerprints)),
        )
    }
}

//prior art: https://benui.ca/unreal/uproperty/#general-points
//display name
//category
//tooltip
//min
//max
//clamp min
//clamp max
//units
//array fixed size?
//which field to use to describe the element (for arrays that can't be inlined). Could be like "Field {x} is {y}"
//rgb/xyz/rgba/xyzw
//bitmasks/bitflags?
//deprecation
//allowed classes for references. can we have some concept of interfaces?
//code generation
//if it is an asset that can be created in ui
//if it is import data
//hide in inspector?

#[derive(Default, Debug, Clone)]
pub struct SchemaDefRecordFieldMarkup {
    // If set, we use this name instead of the class name in most UI
    pub display_name: Option<String>,
    // Additional documentation that may show in a tooltip, for example
    pub description: Option<String>,

    // Groups related fields together
    pub category: Option<String>,

    // Clamp min/max cause the UI to force numeric values to be within the given range
    pub clamp_min: Option<f64>,
    pub clamp_max: Option<f64>,

    // ui min/max sets the begin/end point for sliding values but user can still set a value outside
    // this range
    pub ui_min: Option<f64>,
    pub ui_max: Option<f64>,
}

impl SchemaDefRecordFieldMarkup {
    pub fn clamp_min(&self) -> f64 {
        self.clamp_min.unwrap_or(f64::MIN)
    }

    pub fn clamp_max(&self) -> f64 {
        self.clamp_max.unwrap_or(f64::MAX)
    }

    pub fn ui_min(&self) -> f64 {
        // The greater of clamp/ui min
        self.clamp_min
            .unwrap_or(f64::MIN)
            .max(self.ui_min.unwrap_or(f64::MIN))
    }

    pub fn ui_max(&self) -> f64 {
        // The lesser of clamp/ui max
        self.clamp_max
            .unwrap_or(f64::MAX)
            .min(self.ui_max.unwrap_or(f64::MAX))
    }

    pub fn has_min_bound(&self) -> bool {
        self.ui_min.is_some() || self.clamp_min.is_some()
    }

    pub fn has_max_bound(&self) -> bool {
        self.ui_max.is_some() || self.clamp_max.is_some()
    }
}

#[derive(Debug)]
pub struct SchemaDefRecordField {
    pub(super) field_name: String,
    pub(super) field_uuid: Uuid,
    pub(super) aliases: Vec<String>,
    pub(super) field_type: SchemaDefType,
    pub(super) markup: SchemaDefRecordFieldMarkup,
}

impl SchemaDefRecordField {
    pub fn new(
        field_name: String,
        field_uuid: Uuid,
        aliases: Vec<String>,
        field_type: SchemaDefType,
        markup: SchemaDefRecordFieldMarkup,
    ) -> SchemaDefValidationResult<Self> {
        Ok(SchemaDefRecordField {
            field_name,
            field_uuid,
            aliases,
            field_type,
            markup,
        })
    }

    fn apply_type_aliases(
        &mut self,
        aliases: &HashMap<String, String>,
    ) {
        self.field_type.apply_type_aliases(aliases);
    }

    fn collect_all_related_types(
        &self,
        types: &mut HashSet<String>,
    ) {
        self.field_type.collect_all_related_types(types);
    }

    fn partial_hash<T: Hasher>(
        &self,
        hasher: &mut T,
    ) {
        // Should this be field UUID instead?
        self.field_name.hash(hasher);
        self.field_type.partial_hash(hasher);
    }

    fn to_schema(
        &self,
        named_types: &HashMap<String, SchemaDefNamedType>,
        fingerprints: &HashMap<String, SchemaFingerprint>,
    ) -> SchemaRecordField {
        SchemaRecordField::new(
            self.field_name.clone(),
            self.field_uuid,
            self.aliases.clone().into_boxed_slice(),
            self.field_type.to_schema(named_types, fingerprints),
            self.markup.clone(),
        )
    }
}

#[derive(Default, Debug, Clone)]
pub struct SchemaDefRecordMarkup {
    // If set, we use this name instead of the class name in most UI
    pub display_name: Option<String>,

    pub default_thumbnail: Option<PathBuf>,

    // Tags can be used to query for a list of records that meet some criteria
    pub tags: HashSet<String>,
    //description: String,
}

//TODO: Verify we don't have dupe field names
#[derive(Debug)]
pub struct SchemaDefRecord {
    pub(super) type_name: String,
    pub(super) type_uuid: Uuid,
    pub(super) aliases: Vec<String>,
    pub(super) fields: Vec<SchemaDefRecordField>,
    pub(super) markup: SchemaDefRecordMarkup,
}

impl SchemaDefRecord {
    pub fn new(
        type_name: String,
        type_uuid: Uuid,
        aliases: Vec<String>,
        fields: Vec<SchemaDefRecordField>,
        markup: SchemaDefRecordMarkup,
    ) -> SchemaDefValidationResult<Self> {
        // Check names are unique
        for i in 0..fields.len() {
            for j in 0..i {
                if fields[i].field_name == fields[j].field_name {
                    Err(SchemaDefValidationError::DuplicateFieldName(
                        type_name.clone(),
                        fields[i].field_name.to_string(),
                    ))?;
                }
            }
        }

        Ok(SchemaDefRecord {
            type_name,
            type_uuid,
            aliases,
            fields,
            markup,
        })
    }

    pub(crate) fn fields(&self) -> &Vec<SchemaDefRecordField> {
        &self.fields
    }

    fn apply_type_aliases(
        &mut self,
        aliases: &HashMap<String, String>,
    ) {
        for field in &mut self.fields {
            field.apply_type_aliases(aliases);
        }
    }

    fn collect_all_related_types(
        &self,
        types: &mut HashSet<String>,
    ) {
        types.insert(self.type_name.clone());
        for field in &self.fields {
            field.collect_all_related_types(types);
        }
    }

    fn partial_hash<T: Hasher>(
        &self,
        hasher: &mut T,
    ) {
        // should this be type_uuid instead?
        self.type_name.hash(hasher);

        // should this sort by field_uuid instead?
        let mut sorted_fields: Vec<_> = self.fields.iter().collect();
        sorted_fields.sort_by_key(|x| &x.field_name);

        for field in sorted_fields {
            //println!("field {}", field.field_name);
            field.partial_hash(hasher);
        }
    }

    fn to_schema(
        &self,
        named_types: &HashMap<String, SchemaDefNamedType>,
        fingerprints: &HashMap<String, SchemaFingerprint>,
    ) -> SchemaRecord {
        let fingerprint = *fingerprints.get(&self.type_name).unwrap();

        let mut fields = Vec::with_capacity(self.fields.len());
        for field in &self.fields {
            fields.push(field.to_schema(named_types, fingerprints));
        }

        SchemaRecord::new(
            self.type_name.clone(),
            self.type_uuid,
            fingerprint,
            self.aliases.clone().into_boxed_slice(),
            fields,
            self.markup.clone(),
        )
    }
}

#[derive(Debug)]
pub struct SchemaDefEnumSymbol {
    pub(super) symbol_name: String,
    pub(super) symbol_uuid: Uuid,
    pub(super) aliases: Vec<String>,
}

impl SchemaDefEnumSymbol {
    pub fn new(
        symbol_name: String,
        symbol_uuid: Uuid,
        aliases: Vec<String>,
    ) -> SchemaDefValidationResult<Self> {
        Ok(SchemaDefEnumSymbol {
            symbol_name,
            symbol_uuid,
            aliases,
        })
    }

    fn partial_hash<T: Hasher>(
        &self,
        hasher: &mut T,
    ) {
        // should this use symbol_uuid instead?
        self.symbol_name.hash(hasher);
    }

    fn to_schema(&self) -> SchemaEnumSymbol {
        SchemaEnumSymbol::new(
            self.symbol_name.clone(),
            self.symbol_uuid,
            self.aliases.clone().into_boxed_slice(),
        )
    }
}

//TODO: Verify that we don't have dupe symbol names or values
#[derive(Debug)]
pub struct SchemaDefEnum {
    pub(super) type_name: String,
    pub(super) type_uuid: Uuid,
    pub(super) aliases: Vec<String>,
    pub(super) symbols: Vec<SchemaDefEnumSymbol>,
}

impl SchemaDefEnum {
    pub fn new(
        type_name: String,
        type_uuid: Uuid,
        aliases: Vec<String>,
        symbols: Vec<SchemaDefEnumSymbol>,
    ) -> SchemaDefValidationResult<Self> {
        Ok(SchemaDefEnum {
            type_name,
            type_uuid,
            aliases,
            symbols,
        })
    }

    fn apply_type_aliases(
        &mut self,
        _aliases: &HashMap<String, String>,
    ) {
    }

    fn collect_all_related_types(
        &self,
        types: &mut HashSet<String>,
    ) {
        types.insert(self.type_name.clone());
    }

    fn partial_hash<T: Hasher>(
        &self,
        hasher: &mut T,
    ) {
        // should this use type_uuid instead?
        self.type_name.hash(hasher);

        // should this sort by symbol_uuid instead?
        let mut sorted_symbols: Vec<_> = self.symbols.iter().collect();
        sorted_symbols.sort_by(|a, b| a.symbol_name.cmp(&b.symbol_name));

        for symbol in sorted_symbols {
            symbol.partial_hash(hasher);
        }
    }

    fn to_schema(
        &self,
        named_types: &HashMap<String, SchemaFingerprint>,
    ) -> SchemaEnum {
        let fingerprint = *named_types.get(&self.type_name).unwrap();

        let mut symbols = Vec::with_capacity(self.symbols.len());
        for symbol in &self.symbols {
            symbols.push(symbol.to_schema());
        }

        SchemaEnum::new(
            self.type_name.clone(),
            self.type_uuid,
            fingerprint,
            self.aliases.clone().into_boxed_slice(),
            symbols.into_boxed_slice(),
        )
    }
}

#[derive(Debug)]
pub enum SchemaDefType {
    Nullable(Box<SchemaDefType>),
    Boolean,
    I32,
    I64,
    U32,
    U64,
    F32,
    F64,
    Bytes,
    String,
    StaticArray(SchemaDefStaticArray),
    DynamicArray(SchemaDefDynamicArray),
    Map(SchemaDefMap),
    AssetRef(String),  // name of the type
    NamedType(String), // name of the type
}

impl SchemaDefType {
    fn apply_type_aliases(
        &mut self,
        aliases: &HashMap<String, String>,
    ) {
        match self {
            SchemaDefType::Nullable(x) => x.apply_type_aliases(aliases),
            SchemaDefType::Boolean => {}
            SchemaDefType::I32 => {}
            SchemaDefType::I64 => {}
            SchemaDefType::U32 => {}
            SchemaDefType::U64 => {}
            SchemaDefType::F32 => {}
            SchemaDefType::F64 => {}
            SchemaDefType::Bytes => {}
            SchemaDefType::String => {}
            SchemaDefType::StaticArray(x) => x.apply_type_aliases(aliases),
            SchemaDefType::DynamicArray(x) => x.apply_type_aliases(aliases),
            SchemaDefType::Map(x) => x.apply_type_aliases(aliases),
            SchemaDefType::AssetRef(x) => {
                let alias = aliases.get(x);
                if let Some(alias) = alias {
                    *x = alias.clone();
                }
            }
            SchemaDefType::NamedType(x) => {
                let alias = aliases.get(x);
                if let Some(alias) = alias {
                    *x = alias.clone();
                }
            }
        }
    }

    fn collect_all_related_types(
        &self,
        types: &mut HashSet<String>,
    ) {
        match self {
            SchemaDefType::Nullable(x) => x.collect_all_related_types(types),
            SchemaDefType::Boolean => {}
            SchemaDefType::I32 => {}
            SchemaDefType::I64 => {}
            SchemaDefType::U32 => {}
            SchemaDefType::U64 => {}
            SchemaDefType::F32 => {}
            SchemaDefType::F64 => {}
            SchemaDefType::Bytes => {}
            SchemaDefType::String => {}
            SchemaDefType::StaticArray(x) => x.collect_all_related_types(types),
            SchemaDefType::DynamicArray(x) => x.collect_all_related_types(types),
            SchemaDefType::Map(x) => x.collect_all_related_types(types),
            SchemaDefType::AssetRef(x) => {
                types.insert(x.clone());
            }
            SchemaDefType::NamedType(x) => {
                types.insert(x.clone());
            }
        }
    }

    fn partial_hash<T: Hasher>(
        &self,
        hasher: &mut T,
    ) {
        //println!("ty {:?}", self);
        match self {
            SchemaDefType::Nullable(x) => {
                "Nullable".hash(hasher);
                x.partial_hash(hasher);
            }
            SchemaDefType::Boolean => "Boolean".hash(hasher),
            SchemaDefType::I32 => "I32".hash(hasher),
            SchemaDefType::I64 => "I64".hash(hasher),
            SchemaDefType::U32 => "U32".hash(hasher),
            SchemaDefType::U64 => "U64".hash(hasher),
            SchemaDefType::F32 => "F32".hash(hasher),
            SchemaDefType::F64 => "F64".hash(hasher),
            SchemaDefType::Bytes => "Bytes".hash(hasher),
            SchemaDefType::String => "String".hash(hasher),
            SchemaDefType::StaticArray(x) => {
                "StaticArray".hash(hasher);
                x.partial_hash(hasher);
            }
            SchemaDefType::DynamicArray(x) => {
                "DynamicArray".hash(hasher);
                x.partial_hash(hasher);
            }
            SchemaDefType::Map(x) => {
                "Map".hash(hasher);
                x.partial_hash(hasher);
            }
            SchemaDefType::AssetRef(x) => {
                "AssetRef".hash(hasher);
                x.hash(hasher);
            }
            SchemaDefType::NamedType(x) => {
                "NamedType".hash(hasher);
                x.hash(hasher);
            }
        }
    }

    fn to_schema(
        &self,
        named_types: &HashMap<String, SchemaDefNamedType>,
        fingerprints: &HashMap<String, SchemaFingerprint>,
    ) -> Schema {
        match self {
            SchemaDefType::Nullable(x) => {
                Schema::Nullable(Box::new(x.to_schema(named_types, fingerprints)))
            }
            SchemaDefType::Boolean => Schema::Boolean,
            SchemaDefType::I32 => Schema::I32,
            SchemaDefType::I64 => Schema::I64,
            SchemaDefType::U32 => Schema::U32,
            SchemaDefType::U64 => Schema::U64,
            SchemaDefType::F32 => Schema::F32,
            SchemaDefType::F64 => Schema::F64,
            SchemaDefType::Bytes => Schema::Bytes,
            SchemaDefType::String => Schema::String,
            SchemaDefType::StaticArray(x) => {
                Schema::StaticArray(x.to_schema(named_types, fingerprints))
            }
            SchemaDefType::DynamicArray(x) => {
                Schema::DynamicArray(x.to_schema(named_types, fingerprints))
            }
            SchemaDefType::Map(x) => Schema::Map(x.to_schema(named_types, fingerprints)),
            SchemaDefType::AssetRef(x) => Schema::AssetRef(*fingerprints.get(x).unwrap()),
            SchemaDefType::NamedType(x) => {
                let named_type = named_types.get(x).unwrap();
                match named_type {
                    SchemaDefNamedType::Record(_) => Schema::Record(*fingerprints.get(x).unwrap()),
                    SchemaDefNamedType::Enum(_) => Schema::Enum(*fingerprints.get(x).unwrap()),
                }
            }
        }
    }
}

pub enum SchemaDefNamedType {
    Record(SchemaDefRecord),
    Enum(SchemaDefEnum),
}

impl SchemaDefNamedType {
    pub(super) fn type_name(&self) -> &str {
        match self {
            SchemaDefNamedType::Record(x) => &x.type_name,
            SchemaDefNamedType::Enum(x) => &x.type_name,
        }
    }

    pub(super) fn type_uuid(&self) -> Uuid {
        match self {
            SchemaDefNamedType::Record(x) => x.type_uuid,
            SchemaDefNamedType::Enum(x) => x.type_uuid,
        }
    }

    pub(super) fn aliases(&self) -> &[String] {
        match self {
            SchemaDefNamedType::Record(x) => &x.aliases,
            SchemaDefNamedType::Enum(x) => &x.aliases,
        }
    }

    pub(super) fn apply_type_aliases(
        &mut self,
        aliases: &HashMap<String, String>,
    ) {
        match self {
            SchemaDefNamedType::Record(x) => x.apply_type_aliases(aliases),
            SchemaDefNamedType::Enum(x) => x.apply_type_aliases(aliases),
        }
    }

    pub(super) fn collect_all_related_types(
        &self,
        types: &mut HashSet<String>,
    ) {
        match self {
            SchemaDefNamedType::Record(x) => x.collect_all_related_types(types),
            SchemaDefNamedType::Enum(x) => x.collect_all_related_types(types),
        }
    }

    pub(super) fn partial_hash<T: Hasher>(
        &self,
        hasher: &mut T,
    ) {
        match self {
            SchemaDefNamedType::Record(x) => {
                //println!("record");
                "record".hash(hasher);
                x.partial_hash(hasher);
            }
            SchemaDefNamedType::Enum(x) => {
                "enum".hash(hasher);
                x.partial_hash(hasher);
            }
        }
    }

    pub(super) fn to_schema(
        &self,
        named_types: &HashMap<String, SchemaDefNamedType>,
        fingerprints: &HashMap<String, SchemaFingerprint>,
    ) -> SchemaNamedType {
        match self {
            SchemaDefNamedType::Record(x) => {
                SchemaNamedType::Record(x.to_schema(named_types, fingerprints))
            }
            SchemaDefNamedType::Enum(x) => SchemaNamedType::Enum(x.to_schema(fingerprints)),
        }
    }
}