Module ion_schema::isl

source ·
Expand description

Provides a way to construct ISL types/constraints programmatically.

This module consists of three submodules that help constructing ISL types/constraint:

  • isl_type module represents a schema type IslType which converts given ion content in the schema file into an ISL types(not-yet-resolved types). It stores IslConstraints defined within the given type.

  • isl_import module represents a schema import IslImport which converts given ion content in the schema file into an ISL import. It stores schema id, an optional type that needs to be imported and an optional alias to that type.

  • isl_constraint module represents schema constraints IslConstraint which converts given ion content in the schema file into an ISL constraint(not-yet-resolved constraints).

  • isl_type_reference module provides a schema type reference. The type reference grammar is defined in the Ion Schema Specification

§Example usage of isl module to create an IslSchema using IslType:

use ion_schema::isl::isl_constraint::v_1_0::{all_of, type_constraint};
use ion_schema::isl::isl_type::v_1_0::named_type;
use ion_schema::isl::isl_type_reference::v_1_0::{anonymous_type_ref, named_type_ref};
use ion_schema::isl::IslSchema;
use ion_schema::system::SchemaSystem;

// below code represents an ISL type:
// type:: {
//      name:my_type_name,
//      type: int,
//      all_of: [
//          { type: bool }
//      ]
//  }
let isl_type = named_type(
    // represents the `name` of the defined type
    "my_type_name".to_owned(),
    vec![
        // represents the `type: int` constraint
        type_constraint(
            named_type_ref("int")
        ),
        // represents `all_of` with anonymous type `{ type: bool }` constraint
        all_of(
            vec![
                anonymous_type_ref(
                    vec![
                        type_constraint(
                            named_type_ref("bool")
                        )
                    ]
                )
            ]
        )
    ]
);

// create an ISL schema using above IslType
let isl_schema = IslSchema::schema_v_1_0("my_schema", vec![], vec![isl_type.to_owned()], vec![], vec![]);

// create a schema with resolved type references from given IslSchema using SchemaSystem
let mut schema_system = SchemaSystem::new(vec![]); // no authorities added
let schema = schema_system.load_schema_from_isl_schema_v1_0(isl_schema);

assert!(schema.is_ok());

// verify if the generated schema contains the correct type
assert!(schema.unwrap().get_type("my_type_name").is_some())

§Example of serializing a programmatically constructed schema into a schema file:

use ion_rs::{IonWriter, TextWriterBuilder};
use ion_schema::isl::{isl_type::v_1_0::*, isl_constraint::v_1_0::*, isl_type_reference::v_1_0::*, IslSchema, WriteToIsl};
use ion_schema::schema::Schema;
use ion_schema::system::SchemaSystem;

let isl_type = named_type(
    // represents the `name` of the defined type
    "my_type_name".to_owned(),
    vec![
        // represents the `type: int` constraint
        type_constraint(
            named_type_ref("int")
        ),
        // represents `all_of` with anonymous type `{ type: bool }` constraint
        all_of(
            vec![
                anonymous_type_ref(
                    vec![
                        type_constraint(
                            named_type_ref("bool")
                        )
                    ]
                )
            ]
        )
    ]
);

// create an ISL schema using above IslType
let isl_schema = IslSchema::schema_v_1_0("my_schema", vec![], vec![isl_type.to_owned()], vec![], vec![]);

// initiate an Ion pretty text writer
let mut buffer = Vec::new();
let mut writer = TextWriterBuilder::pretty().build(&mut buffer).unwrap();

// write the previously constructed ISL model into a schema file using `write_to`
let write_schema_result = isl_schema.write_to(&mut writer);
assert!(write_schema_result.is_ok());

// The above written schema file looks like following:
// $ion_schema_1_0
// schema_header::{}
// type::{
//    name: my_type_name,
//   type: int,
//   all_of: [
//       {
//           type: bool
//       }
//   ]
// }
// schema_footer::{}

Note that all the above functions to construct a type, constraint and type reference comes from v_1_0 module which represents functions for ISL 1.0. In order to programmatically construct ISL 2.0 types, constraints and type references use v_2_0 module.

Modules§

Structs§

  • Provides an internal representation of an schema file

Enums§

  • Represents Ion Schema Language Versions Currently it support v1.0 and v2.0

Traits§

  • Provides a method to write an ISL model using an Ion writer