amazon-ion / ion-schema-rust

Rust implementation of Ion Schema
https://amazon-ion.github.io/ion-schema/sandbox
Apache License 2.0
12 stars 6 forks source link

adds implementation for `WriteToIsl` #183

Closed desaikd closed 1 year ago

desaikd commented 1 year ago

Issue #173

Description of changes:

This PR works on adding implementation of WriteToIsl which serializes a schema model into a schema file using Ion writer.

Example:

Here is a quick example of how to serialize a schema model using Ion writer.

// construct an ISL type programmatically
let isl_type = named_type(
    // represents the `name` of the defined type
    "my_number_type".to_owned(),
    vec![
        // represents `one_of` constraint
        one_of(vec![
            named_type_ref("int"),
            named_type_ref("float"),
            named_type_ref("decimal"),
        ]),
    ],
);

// create an ISL schema using above `isl_type`
let isl_schema = IslSchema::schema_v_1_0(
    "my_schema.isl", // represents schem id which is usually same as the schema file name
    vec![], // represents imports
    vec![isl_type.to_owned()],
    vec![], // represents inline imports
    vec![], // represents open content
);

// use an Ion Writer to write the ISL model
let mut buffer = Vec::new();
let mut writer = TextWriterBuilder::pretty().build(&mut buffer)?;
isl_schema.write_to(&mut writer)?;

// write into schema file
let mut file = File::create("my_schema.isl")?;
file.write_all(writer.output().as_bytes())?;

Output:

$ion_schema_1_0
schema_header::{}
type::{
    name: "my_number_type",
    one_of: [
        int,
        float,
        decimal
    ]
}
schema_footer::{}

List of changes:


By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.