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
use crate::constraint::Constraint;
use crate::ion_path::IonPath;
use crate::isl::isl_constraint::IslConstraintValue;
use crate::isl::isl_type::IslType;
use crate::isl::IslVersion;
use crate::result::{invalid_schema_error, IonSchemaResult, ValidationResult};
use crate::system::{PendingTypes, TypeId, TypeStore};
use crate::violation::{Violation, ViolationCode};
use crate::IonSchemaElement;
use ion_rs::element::Element;
use ion_rs::IonType;
use ion_rs::Symbol;
use std::fmt::{Display, Formatter};
use std::sync::Arc;

/// Provides validation for type definition
pub(crate) trait TypeValidator {
    /// If the specified value violates one or more of this type's constraints,
    /// returns `false`, otherwise `true`
    fn is_valid(
        &self,
        value: &IonSchemaElement,
        type_store: &TypeStore,
        ion_path: &mut IonPath,
    ) -> bool;

    /// Returns `Err(violation)` with details as to which constraints were violated,
    /// otherwise returns `Ok(())` indicating no violations were found during the validation
    fn validate(
        &self,
        value: &IonSchemaElement,
        type_store: &TypeStore,
        ion_path: &mut IonPath,
    ) -> ValidationResult;
}

// Provides a public facing schema type which has a reference to TypeStore
// to get the underlying TypeDefinitionKind from TypeStore
/// Represents a top level ISL type definition
#[derive(Debug, Clone)]
pub struct TypeDefinition {
    id: TypeId,
    type_store: Arc<TypeStore>,
}

impl TypeDefinition {
    pub(crate) fn new(id: TypeId, type_store: Arc<TypeStore>) -> Self {
        Self { id, type_store }
    }

    pub fn id(&self) -> TypeId {
        self.id
    }

    /// Provides the validation for the given value based on this schema type
    /// ```
    /// use ion_rs::element::Element;
    /// use ion_schema::IonSchemaElement;
    /// use ion_schema::authority::{FileSystemDocumentAuthority, DocumentAuthority};
    /// use ion_schema::system::SchemaSystem;
    /// use ion_schema::result::IonSchemaResult;
    /// use std::path::Path;
    ///
    /// fn main() -> IonSchemaResult<()> {
    ///     // create an IonSchemaElement from an Element
    ///     use ion_schema::authority::MapDocumentAuthority;
    ///     let owned_element: Element = 4.into();
    ///     let document: Vec<Element> = vec![4.into(), "hello".to_string().into(), true.into()];
    ///
    ///     let map_authority = [
    ///         (
    ///            "sample.isl",
    ///             r#"
    ///                 type::{
    ///                     name: my_int,
    ///                     type: int,
    ///                 }
    ///             "#
    ///         )   
    ///     ];
    ///
    ///     // create a vector of authorities and construct schema system
    ///     // this example uses above mentioned map as the authority
    ///     let authorities: Vec<Box<dyn DocumentAuthority>> = vec![Box::new(
    ///             MapDocumentAuthority::new(map_authority),
    ///         )];
    ///     let mut schema_system = SchemaSystem::new(authorities);
    ///
    ///     // use this schema_system to load a schema as following
    ///     let schema = schema_system.load_schema("sample.isl")?;
    ///
    ///     // unwrap() here because we know that the `my_int` type exists in sample.isl
    ///     let type_ref = schema.get_type("my_int").unwrap();
    ///
    ///     assert!(type_ref.validate(&owned_element).is_ok()); // 4 is valid for `my_int`
    ///     assert!(type_ref.validate(&document).is_err()); // document type is invalid for `my_int` type
    ///     Ok(())
    /// }
    /// ```
    pub fn validate<I: Into<IonSchemaElement>>(&self, value: I) -> ValidationResult {
        let type_def = self.type_store.get_type_by_id(self.id).unwrap();

        // convert given IonSchemaElement to an Element
        let schema_element: IonSchemaElement = value.into();

        type_def.validate(&schema_element, &self.type_store, &mut IonPath::default())
    }
}

/// Represents a [`BuiltInTypeDefinition`] which stores a resolved builtin ISl type using [`TypeStore`]
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum BuiltInTypeDefinition {
    Atomic(IonType, Nullability),
    Derived(TypeDefinitionImpl),
}

/// Represents whether an atomic built-in type is nullable or not
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Nullability {
    Nullable,
    NotNullable,
}

impl BuiltInTypeDefinition {
    pub(crate) fn parse_from_isl_type(
        isl_version: IslVersion,
        isl_type: &IslType,
        type_store: &mut TypeStore,
        pending_types: &mut PendingTypes,
    ) -> IonSchemaResult<Self> {
        let mut constraints = vec![];

        // parses an isl_type to a TypeDefinition
        let type_name = isl_type.name();

        // convert IslConstraint to Constraint
        for isl_constraint in isl_type.constraints() {
            // For built in types, open_content is set as true as Ion Schema by default allows open content
            let constraint = Constraint::resolve_from_isl_constraint(
                isl_version,
                &isl_constraint.constraint_value,
                type_store,
                pending_types,
                true,
            )?;
            constraints.push(constraint);
        }

        let builtin_type_def = BuiltInTypeDefinition::Derived(TypeDefinitionImpl::new(
            type_name.to_owned(),
            constraints,
            None,
        ));
        Ok(builtin_type_def)
    }
}

impl TypeValidator for BuiltInTypeDefinition {
    fn is_valid(
        &self,
        value: &IonSchemaElement,
        type_store: &TypeStore,
        ion_path: &mut IonPath,
    ) -> bool {
        let violation = self.validate(value, type_store, ion_path);
        violation.is_ok()
    }

    fn validate(
        &self,
        value: &IonSchemaElement,
        type_store: &TypeStore,
        ion_path: &mut IonPath,
    ) -> ValidationResult {
        match &self {
            BuiltInTypeDefinition::Atomic(ion_type, is_nullable) => {
                // atomic types doesn't include document type
                match value {
                    IonSchemaElement::SingleElement(element) => {
                        if *is_nullable == Nullability::NotNullable && element.is_null() {
                            return Err(Violation::new(
                                "type_constraint",
                                ViolationCode::InvalidNull,
                                format!("expected type {ion_type:?} doesn't allow null"),
                                ion_path,
                            ));
                        }
                        if element.ion_type() != *ion_type {
                            return Err(Violation::new(
                                "type_constraint",
                                ViolationCode::TypeMismatched,
                                format!(
                                    "expected type {:?}, found {:?}",
                                    ion_type,
                                    element.ion_type()
                                ),
                                ion_path,
                            ));
                        }

                        Ok(())
                    }
                    IonSchemaElement::Document(document) => Err(Violation::new(
                        "type_constraint",
                        ViolationCode::TypeMismatched,
                        format!("expected type {ion_type:?}, found document"),
                        ion_path,
                    )),
                }
            }
            BuiltInTypeDefinition::Derived(other_type) => {
                if other_type.name() == &Some("document".to_owned()) {
                    // Verify whether the given derived type is document
                    // And check if it is using enum variant IonSchemaIonElement::Document
                    if Option::is_none(&value.as_document()) {
                        return Err(Violation::new(
                            "type_constraint",
                            ViolationCode::TypeMismatched,
                            format!(
                                "expected type document found {:?}",
                                value.as_element().unwrap().ion_type()
                            ),
                            ion_path,
                        ));
                    }
                    return Ok(());
                }
                // if it is not a document type do validation using the type definition
                other_type.validate(value, type_store, ion_path)
            }
        }
    }
}

impl Display for BuiltInTypeDefinition {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match &self {
            BuiltInTypeDefinition::Atomic(ion_type, _) => write!(f, "{ion_type}"),
            BuiltInTypeDefinition::Derived(type_def) => write!(f, "{type_def}"),
        }
    }
}

/// Represents a [`TypeDefinitionKind`] which stores a resolved ISL type using [`TypeStore`]
#[derive(Debug, Clone, PartialEq)]
pub(crate) enum TypeDefinitionKind {
    Named(TypeDefinitionImpl),
    Anonymous(TypeDefinitionImpl),
    BuiltIn(BuiltInTypeDefinition),
}

impl TypeDefinitionKind {
    /// Creates a named [`TypeDefinitionKind`] using the [`Constraint`]s defined within it
    pub fn named<A: Into<String>, B: Into<Vec<Constraint>>>(
        name: A,
        constraints: B,
    ) -> TypeDefinitionKind {
        TypeDefinitionKind::Named(TypeDefinitionImpl::new(
            Some(name.into()),
            constraints.into(),
            None,
        ))
    }

    /// Provides a boolean that represents whether this type is deferred type or not
    pub fn is_deferred_type_def(&self) -> bool {
        match self {
            TypeDefinitionKind::Named(type_def) => type_def.is_deferred_type_def,
            _ => false,
        }
    }

    /// Creates an anonymous [`TypeDefinitionKind`] using the [`Constraint`]s defined within it
    pub fn anonymous<A: Into<Vec<Constraint>>>(constraints: A) -> TypeDefinitionKind {
        TypeDefinitionKind::Anonymous(TypeDefinitionImpl::new(None, constraints.into(), None))
    }

    /// Provides the underlying constraints of [`TypeDefinitionKind`]
    pub fn constraints(&self) -> &[Constraint] {
        match &self {
            TypeDefinitionKind::Named(named_type) => named_type.constraints(),
            TypeDefinitionKind::Anonymous(anonymous_type) => anonymous_type.constraints(),
            _ => &[],
        }
    }

    /// Provides the validation result for base built in type of the type definition with the given value
    // This method is only used by nullable type reference for validation, it searches for a base type name which is built in type and nullable.
    // It returns the result of validation for that nullable base type.
    pub fn is_valid_for_base_nullable_type(
        &self,
        value: &IonSchemaElement,
        type_store: &TypeStore,
        ion_path: &mut IonPath,
    ) -> bool {
        // get a nullable built in base type name which can be used to perform validation to check for correct `null.*` type
        let built_in_type_name = match self {
            TypeDefinitionKind::Named(_) | TypeDefinitionKind::Anonymous(_) => {
                // only built in type names are needed for base type
                None
            }
            TypeDefinitionKind::BuiltIn(built_int_type) => match built_int_type {
                BuiltInTypeDefinition::Atomic(ion_type, nullability) => {
                    Some(format!("${ion_type}"))
                }
                BuiltInTypeDefinition::Derived(type_def) => {
                    let type_name = type_def.name().as_ref().unwrap().to_string();
                    if !type_name.starts_with('$') {
                        Some(format!("${type_name}"))
                    } else {
                        Some(type_name)
                    }
                }
            },
        }
        .unwrap(); // safe unwrap() because nullable type references are only used for built in types

        // unwrap() here are safe because it has been verified while constructing the schema that built type definition exist in the type store
        let type_def = type_store
            .get_type_by_id(
                type_store
                    .get_builtin_type_id(built_in_type_name.as_str())
                    .unwrap(),
            )
            .unwrap();

        type_def.is_valid(value, type_store, ion_path)
    }
}

impl Display for TypeDefinitionKind {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match &self {
            TypeDefinitionKind::Named(named_type_def) => {
                write!(f, "{named_type_def}")
            }
            TypeDefinitionKind::Anonymous(anonymous_type_def) => {
                write!(f, "{anonymous_type_def}")
            }
            TypeDefinitionKind::BuiltIn(builtin_type_def) => {
                write!(f, "{builtin_type_def}")
            }
        }
    }
}

impl TypeValidator for TypeDefinitionKind {
    fn is_valid(
        &self,
        value: &IonSchemaElement,
        type_store: &TypeStore,
        ion_path: &mut IonPath,
    ) -> bool {
        let violation = self.validate(value, type_store, ion_path);
        violation.is_ok()
    }

    fn validate(
        &self,
        value: &IonSchemaElement,
        type_store: &TypeStore,
        ion_path: &mut IonPath,
    ) -> ValidationResult {
        match self {
            TypeDefinitionKind::Named(named_type) => {
                named_type.validate(value, type_store, ion_path)
            }
            TypeDefinitionKind::Anonymous(anonymous_type) => {
                anonymous_type.validate(value, type_store, ion_path)
            }
            TypeDefinitionKind::BuiltIn(built_in_type) => {
                built_in_type.validate(value, type_store, ion_path)
            }
        }
    }
}

/// A [`TypeDefinitionImpl`] consists of an optional name and zero or more constraints.
#[derive(Debug, Clone)]
pub(crate) struct TypeDefinitionImpl {
    name: Option<String>,
    constraints: Vec<Constraint>,
    // `is_deferred_type_def` indicates if this is a deferred type def which will be resolved later
    // e.g.
    // ```
    // type:: {
    //  name: foo,
    //  type: bar,
    // }
    // type:: {
    //  name: bar,
    //  type: int
    // }
    // ```
    // For above example, `bar` will be saved as deferred type definition until we resolve the definition of `bar`
    is_deferred_type_def: bool,
    // Represents the ISL type struct in string format, this will be used for violation messages
    isl_type_struct: Option<Element>,
}

impl TypeDefinitionImpl {
    pub fn new(
        name: Option<String>,
        constraints: Vec<Constraint>,
        isl_type_struct: Option<Element>,
    ) -> Self {
        Self {
            name,
            constraints,
            is_deferred_type_def: false,
            isl_type_struct,
        }
    }

    pub fn new_deferred_type_def(name: String) -> Self {
        Self {
            name: Some(name),
            constraints: vec![],
            is_deferred_type_def: true,
            isl_type_struct: None,
        }
    }

    pub fn name(&self) -> &Option<String> {
        &self.name
    }

    pub fn with_name(self, alias: String) -> Self {
        Self {
            name: Some(alias),
            constraints: self.constraints,
            is_deferred_type_def: self.is_deferred_type_def,
            isl_type_struct: None,
        }
    }

    pub fn is_deferred_type_def(&self) -> bool {
        self.is_deferred_type_def
    }

    pub fn constraints(&self) -> &[Constraint] {
        &self.constraints
    }

    /// Parse constraints inside an [`IslType`] to a schema type definition, update the [`PendingTypes`]
    /// and return its [`TypeId`] if the conversion was successful, otherwise return an [`IonSchemaError`]
    ///
    /// [`IonSchemaError`]: crate::result::IonSchemaError
    pub(crate) fn parse_from_isl_type_and_update_pending_types(
        isl_version: IslVersion,
        isl_type: &IslType,
        type_store: &mut TypeStore,
        pending_types: &mut PendingTypes,
    ) -> IonSchemaResult<TypeId> {
        let mut constraints = vec![];

        // parses an isl_type to a TypeDefinition
        let type_name = isl_type.name();

        if let Some(type_name) = type_name {
            if let Some(type_def) = type_store.get_type_def_by_name(type_name) {
                if isl_version == IslVersion::V2_0 && !type_def.is_deferred_type_def() {
                    return invalid_schema_error(format!(
                        "The schema document can not have two type definitions with same name: {}",
                        type_name
                    ));
                }
            }
            // add parent information for named type
            pending_types.add_parent(type_name.to_owned(), type_store);
        }

        // add this unresolved type to context for type_id
        let type_id = pending_types.add_type(type_store, type_name.to_owned());

        // convert IslConstraint to Constraint
        let mut found_type_constraint = false;
        for isl_constraint in isl_type.constraints() {
            if let IslConstraintValue::Type(_) = isl_constraint.constraint_value {
                found_type_constraint = true;
            }

            let constraint = Constraint::resolve_from_isl_constraint(
                isl_version,
                &isl_constraint.constraint_value,
                type_store,
                pending_types,
                isl_type.is_open_content_allowed(),
            )?;
            constraints.push(constraint);
        }

        let isl_struct = isl_type.isl_type_struct.as_ref();
        // add `type: any` as a default type constraint if there is no type constraint found
        if !found_type_constraint && isl_version == IslVersion::V1_0 {
            // set the isl type name for any error that is returned while parsing its constraints
            let isl_type_name = match type_name.to_owned() {
                Some(name) => name,
                None => match isl_struct {
                    None => "".to_owned(),
                    Some(isl_type_struct) => format!("{isl_type_struct}"),
                },
            };

            let isl_constraint: IslConstraintValue =
                    // default type for ISL 1.0 is `any`
                    IslConstraintValue::from_ion_element(
                        isl_version,
                        "type",
                        &Element::symbol(Symbol::from("any")),
                        &isl_type_name,
                        &mut vec![],
                    )?;

            let constraint = Constraint::resolve_from_isl_constraint(
                isl_version,
                &isl_constraint,
                type_store,
                pending_types,
                true, // by default Ion Schema allows open content
            )?;
            constraints.push(constraint);
        }

        let type_def = TypeDefinitionImpl::new(
            type_name.to_owned(),
            constraints,
            isl_type.isl_type_struct.to_owned(),
        );

        // actual type id is the type id with respect to type store length
        let actual_type_id;

        if type_name.is_some() {
            // update with this resolved type_def to context for type_id
            actual_type_id = pending_types.update_named_type(
                type_id,
                type_name.as_ref().unwrap(),
                type_def,
                type_store,
            );

            // clear parent information from type_store as the type is already added in the type_store now
            pending_types.clear_parent();
        } else {
            // update with this resolved type_def to context for type_id
            actual_type_id = pending_types.update_anonymous_type(type_id, type_def, type_store);
        }

        Ok(actual_type_id)
    }
}

impl PartialEq for TypeDefinitionImpl {
    fn eq(&self, other: &Self) -> bool {
        self.name() == other.name() && self.constraints == other.constraints()
    }
}

impl TypeValidator for TypeDefinitionImpl {
    fn is_valid(
        &self,
        value: &IonSchemaElement,
        type_store: &TypeStore,
        ion_path: &mut IonPath,
    ) -> bool {
        let violation = self.validate(value, type_store, ion_path);
        violation.is_ok()
    }

    fn validate(
        &self,
        value: &IonSchemaElement,
        type_store: &TypeStore,
        ion_path: &mut IonPath,
    ) -> ValidationResult {
        let mut violations: Vec<Violation> = vec![];
        let type_name = match self.name() {
            None => match self.isl_type_struct.as_ref() {
                None => "".to_owned(),
                Some(anonymous_struct) => {
                    format!("{anonymous_struct}")
                }
            },
            Some(name) => name.to_owned(),
        };
        for constraint in self.constraints() {
            if let Err(violation) = constraint.validate(value, type_store, ion_path) {
                violations.push(violation);
            }
        }
        if violations.is_empty() {
            return Ok(());
        }
        Err(Violation::with_violations(
            type_name,
            ViolationCode::TypeConstraintsUnsatisfied,
            "value didn't satisfy type constraint(s)",
            ion_path,
            violations,
        ))
    }
}

impl Display for TypeDefinitionImpl {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let type_def_name = match &self.name {
            None => match &self.isl_type_struct {
                None => "".to_owned(),
                Some(type_name) => format!("{type_name}"),
            },
            Some(type_name) => type_name.to_owned(),
        };

        write!(f, "{type_def_name}")
    }
}

#[cfg(test)]
mod type_definition_tests {
    use super::*;
    use crate::constraint::Constraint;
    use crate::isl::isl_constraint::v_1_0::*;
    use crate::isl::isl_type::v_1_0::*;
    use crate::isl::isl_type::IslType;
    use crate::isl::isl_type_reference::v_1_0::*;
    use crate::isl::ranges::*;
    use crate::isl::util::Ieee754InterchangeFormat;
    use crate::isl::util::TimestampPrecision;
    use crate::isl::*;
    use crate::system::PendingTypes;

    use rstest::*;
    use std::collections::HashSet;

    // TODO: Remove type ids for assertion to make tests more readable
    #[rstest(
    isl_type, type_def,
    case::type_constraint_with_anonymous_type(
        /* For a schema with single anonymous type as below:
            { type: int }
         */
        anonymous_type([type_constraint(named_type_ref("int"))]),
    TypeDefinitionKind::anonymous([Constraint::type_constraint(0)])
    ),
    case::type_constraint_with_named_type(
        /* For a schema with named type as below:
            { name: my_int, type: int }
         */
        named_type("my_int", [type_constraint(named_type_ref("int"))]),
    TypeDefinitionKind::named("my_int", [Constraint::type_constraint(0)])
    ),
    case::type_constraint_with_self_reference_type(
        /* For a schema with self reference type as below:
            { name: my_int, type: my_int }
         */
        named_type("my_int", [type_constraint(named_type_ref("my_int"))]),
    TypeDefinitionKind::named("my_int", [Constraint::type_constraint(35)])
    ),
    case::type_constraint_with_nested_self_reference_type(
        /* For a schema with nested self reference type as below:
            { name: my_int, type: { type: my_int } }
         */
        named_type("my_int", [type_constraint(anonymous_type_ref([type_constraint(named_type_ref("my_int"))]))]),
    TypeDefinitionKind::named("my_int", [Constraint::type_constraint(36)]) // 0-35 are built-in types which are preloaded to the type_store
    ),
    case::type_constraint_with_nested_type(
        /* For a schema with nested types as below:
            { name: my_int, type: { type: int } }
         */
        named_type("my_int", [type_constraint(anonymous_type_ref([type_constraint(named_type_ref("int"))]))]),
    TypeDefinitionKind::named("my_int", [Constraint::type_constraint(36)])
    ),
    case::type_constraint_with_nested_multiple_types(
        /* For a schema with nested multiple types as below:
            { name: my_int, type: { type: int }, type: { type: my_int } }
         */
        named_type("my_int", [type_constraint(anonymous_type_ref([type_constraint(named_type_ref("int"))])), type_constraint(anonymous_type_ref([type_constraint(named_type_ref("my_int"))]))]),
    TypeDefinitionKind::named("my_int", [Constraint::type_constraint(36), Constraint::type_constraint(37)])
    ),
    case::all_of_constraint(
        /* For a schema with all_of type as below:
            { all_of: [{ type: int }] }
        */
        anonymous_type([all_of([anonymous_type_ref([type_constraint(named_type_ref("int"))])])]),
    TypeDefinitionKind::anonymous([Constraint::all_of([36]), Constraint::type_constraint(34)])
    ),
    case::any_of_constraint(
        /* For a schema with any_of constraint as below:
            { any_of: [{ type: int }, { type: decimal }] }
        */
        anonymous_type([any_of([anonymous_type_ref([type_constraint(named_type_ref("int"))]), anonymous_type_ref([type_constraint(named_type_ref("decimal"))])])]),
    TypeDefinitionKind::anonymous([Constraint::any_of([36, 37]), Constraint::type_constraint(34)])
    ),
    case::one_of_constraint(
        /* For a schema with one_of constraint as below:
            { any_of: [{ type: int }, { type: decimal }] }
        */
        anonymous_type([one_of([anonymous_type_ref([type_constraint(named_type_ref("int"))]), anonymous_type_ref([type_constraint(named_type_ref("decimal"))])])]),
    TypeDefinitionKind::anonymous([Constraint::one_of([36, 37]), Constraint::type_constraint(34)])
    ),
    case::not_constraint(
        /* For a schema with not constraint as below:
            { not: { type: int } }
        */
        anonymous_type([not(anonymous_type_ref([type_constraint(named_type_ref("int"))]))]),
    TypeDefinitionKind::anonymous([Constraint::not(36), Constraint::type_constraint(34)])
    ),
    case::ordered_elements_constraint(
        /* For a schema with ordered_elements constraint as below:
            { ordered_elements: [ symbol, { type: int }, ] }
        */
        anonymous_type([ordered_elements([variably_occurring_type_ref(named_type_ref("symbol"), UsizeRange::new_single_value(1)), variably_occurring_type_ref(anonymous_type_ref([type_constraint(named_type_ref("int"))]), UsizeRange::new_single_value(1))])]),
    TypeDefinitionKind::anonymous([Constraint::ordered_elements([5, 36]), Constraint::type_constraint(34)])
    ),
    case::fields_constraint(
        /* For a schema with fields constraint as below:
            { fields: { name: string, id: int} }
        */
        anonymous_type([fields(vec![("name".to_owned(), variably_occurring_type_ref(named_type_ref("string"), UsizeRange::zero_or_one())), ("id".to_owned(), variably_occurring_type_ref(named_type_ref("int"), UsizeRange::zero_or_one()))].into_iter())]),
    TypeDefinitionKind::anonymous([Constraint::fields(vec![("name".to_owned(), 4), ("id".to_owned(), 0)].into_iter()), Constraint::type_constraint(34)])
    ),
    case::field_names_constraint(
        /* For a schema with field_names constraint as below:
            { field_names: distinct::symbol }
        */
    isl_type::v_2_0::anonymous_type([isl_constraint::v_2_0::field_names(named_type_ref("symbol"), true)]),
    TypeDefinitionKind::anonymous([Constraint::field_names(5, true), Constraint::type_constraint(34)])
    ),
    case::contains_constraint(
        /* For a schema with contains constraint as below:
            { contains: [true, 1, "hello"] }
        */
        anonymous_type([contains([true.into(), 1.into(), "hello".to_owned().into()])]),
    TypeDefinitionKind::anonymous([Constraint::contains([true.into(), 1.into(), "hello".to_owned().into()]), Constraint::type_constraint(34)])
        ),
    case::container_length_constraint(
        /* For a schema with container_length constraint as below:
            { container_length: 3 }
        */
        anonymous_type([container_length(3.into())]),
    TypeDefinitionKind::anonymous([Constraint::container_length(3.into()), Constraint::type_constraint(34)])
    ),
    case::byte_length_constraint(
        /* For a schema with byte_length constraint as below:
            { byte_length: 3 }
        */
        anonymous_type([byte_length(3.into())]),
    TypeDefinitionKind::anonymous([Constraint::byte_length(3.into()), Constraint::type_constraint(34)])
    ),
    case::codepoint_length_constraint(
        /* For a schema with codepoint_length constraint as below:
            { codepoint_length: 3 }
        */
        anonymous_type([codepoint_length(3.into())]),
    TypeDefinitionKind::anonymous([Constraint::codepoint_length(3.into()), Constraint::type_constraint(34)])
    ),
    case::element_constraint(
        /* For a schema with element constraint as below:
            { element: int }
        */
        anonymous_type([element(named_type_ref("int"))]),
    TypeDefinitionKind::anonymous([Constraint::element(0, false), Constraint::type_constraint(34)])
    ),
    case::distinct_element_constraint(
        /* For a schema with distinct element constraint as below:
            { element: distinct::int }
        */
        isl_type::v_2_0::anonymous_type([isl_constraint::v_2_0::element(named_type_ref("int"), true)]),
    TypeDefinitionKind::anonymous([Constraint::element(0, true), Constraint::type_constraint(34)])
    ),
    case::annotations_constraint(
        /* For a schema with annotations constraint as below:
            { annotations: closed::[red, blue, green] }
        */
        anonymous_type([annotations(vec!["closed"], vec![Symbol::from("red").into(), Symbol::from("blue").into(), Symbol::from("green").into()])]),
    TypeDefinitionKind::anonymous([Constraint::annotations(vec!["closed"], vec![Symbol::from("red").into(), Symbol::from("blue").into(), Symbol::from("green").into()]), Constraint::type_constraint(34)])
    ),
    case::annotations_v2_0_constraint(
        /* For a schema with annotations constraint as below:
            { annotations: { container_length: 1 } }
        */
        isl_type::v_2_0::anonymous_type([isl_constraint::v_2_0::annotations(isl_type_reference::v_2_0::anonymous_type_ref([isl_constraint::v_2_0::container_length(1.into())]))]),
        TypeDefinitionKind::anonymous([Constraint::annotations_v2_0(36), Constraint::type_constraint(34)])
    ),
    case::precision_constraint(
        /* For a schema with precision constraint as below:
            { precision: 3 }
        */
        anonymous_type([precision(3.into())]),
    TypeDefinitionKind::anonymous([Constraint::precision(3.into()), Constraint::type_constraint(34)])
    ),
    case::scale_constraint(
        /* For a schema with scale constraint as below:
            { scale: 2 }
        */
        anonymous_type([scale(2.into())]),
    TypeDefinitionKind::anonymous([Constraint::scale(2.into()), Constraint::type_constraint(34)])
    ),
    case::exponent_constraint(
        /* For a schema with exponent constraint as below:
            { exponent: 2 }
        */
        isl_type::v_2_0::anonymous_type([isl_constraint::v_2_0::exponent(2.into())]),
    TypeDefinitionKind::anonymous([Constraint::exponent(2.into()), Constraint::type_constraint(34)])
    ),
    case::timestamp_precision_constraint(
        /* For a schema with timestamp_precision constraint as below:
            { timestamp_precision: month }
        */
        anonymous_type([timestamp_precision(TimestampPrecisionRange::new_single_value(TimestampPrecision::Month))]),
    TypeDefinitionKind::anonymous([Constraint::timestamp_precision(TimestampPrecision::Month.into()), Constraint::type_constraint(34)])
    ),
    case::valid_values_constraint(
    /* For a schema with valid_values constraint as below:
        { valid_values: [2, 3.5, 5e7, "hello", hi, range::[2, 3]] }
    */
    anonymous_type([valid_values(vec![2.into(), ion_rs::Decimal::new(35, -1).into(), 5e7.into(), "hello".to_owned().into(), Symbol::from("hi").into(), NumberRange::new_inclusive(2.into(), 3.into()).unwrap().into()]).unwrap()]),
    TypeDefinitionKind::anonymous([Constraint::valid_values(vec![2.into(), ion_rs::Decimal::new(35, -1).into(), 5e7.into(), "hello".to_owned().into(), Symbol::from("hi").into(), NumberRange::new_inclusive(2.into(), 3.into()).unwrap().into()], IslVersion::V1_0).unwrap(), Constraint::type_constraint(34)])
    ),
    case::utf8_byte_length_constraint(
        /* For a schema with utf8_byte_length constraint as below:
            { utf8_byte_length: 3 }
        */
        anonymous_type([utf8_byte_length(3.into())]),
    TypeDefinitionKind::anonymous([Constraint::utf8_byte_length(3.into()), Constraint::type_constraint(34)])
    ),
    case::regex_constraint(
        /* For a schema with regex constraint as below:
            { regex: "[abc]" }
        */
        anonymous_type(
            [regex(
                false, // case insensitive
                false, // multiline
                "[abc]".to_string()
            )]
        ),
    TypeDefinitionKind::anonymous([
            Constraint::regex(
                false, // case insensitive
                false, // multiline
                "[abc]".to_string(),
                IslVersion::V1_0
            ).unwrap(),
            Constraint::type_constraint(34)
        ])
    ),
    case::timestamp_offset_constraint(
        /* For a schema with timestamp_offset constraint as below:
            { timestamp_offset: ["-00:00"] }
        */
        anonymous_type(
            [timestamp_offset(vec!["-00:00".try_into().unwrap()])]
        ),
    TypeDefinitionKind::anonymous([Constraint::timestamp_offset(vec!["-00:00".try_into().unwrap()]),
            Constraint::type_constraint(34)
        ])
    ),
    case::ieee754_float_constraint(
        /* For a schema with ieee754_float constraint as below:
            { ieee754_float: binary16 }
        */
        isl_type::v_2_0::anonymous_type([isl_constraint::v_2_0::ieee754_float(Ieee754InterchangeFormat::Binary16)]),
        TypeDefinitionKind::anonymous([Constraint::ieee754_float(Ieee754InterchangeFormat::Binary16), Constraint::type_constraint(34)])
    ),
    )]
    fn isl_type_to_type_definition(isl_type: IslType, type_def: TypeDefinitionKind) {
        // assert if both the TypeDefinitionKind are same in terms of constraints and name
        let type_store = &mut TypeStore::default();
        let pending_types = &mut PendingTypes::default();
        let this_type_def = {
            let type_id = TypeDefinitionImpl::parse_from_isl_type_and_update_pending_types(
                IslVersion::V1_0,
                &isl_type,
                type_store,
                pending_types,
            )
            .unwrap();
            pending_types
                .update_type_store(type_store, None, &HashSet::new())
                .unwrap();
            type_store.get_type_by_id(type_id).unwrap()
        };
        assert_eq!(this_type_def, &type_def);
    }
}