#[macro_use]
extern crate clap;
use clap::{App, ArgMatches};
use ion_rs::WriteConfig;
use ion_rs::{Element, SequenceWriter, StructWriter, TextFormat, ValueWriter, Writer};
use ion_schema::authority::{DocumentAuthority, FileSystemDocumentAuthority};
use ion_schema::result::IonSchemaResult;
use ion_schema::system::SchemaSystem;
use std::fs;
use std::path::Path;
use std::str::from_utf8;
fn main() -> IonSchemaResult<()> {
let yaml = load_yaml!("cli.yaml");
let matches = App::from_yaml(yaml).get_matches();
let (command_name, command_args) = matches.subcommand();
let command_args = command_args.unwrap();
match command_name {
"load" => load(command_args)?,
"validate" => validate(command_args)?,
_ => eprintln!("command name: {} not found", command_name),
}
Ok(())
}
fn load(command_args: &ArgMatches) -> IonSchemaResult<()> {
let authorities: Vec<_> = command_args.values_of("directories").unwrap().collect();
let schema_id = command_args.value_of("schema").unwrap();
let mut document_authorities: Vec<Box<dyn DocumentAuthority>> = vec![];
for authority in authorities {
document_authorities.push(Box::new(FileSystemDocumentAuthority::new(Path::new(
authority,
))))
}
let mut schema_system = SchemaSystem::new(document_authorities);
println!("Schema: {:#?}", schema_system.load_schema(schema_id)?);
Ok(())
}
fn validate(command_args: &ArgMatches) -> IonSchemaResult<()> {
let authorities: Vec<_> = command_args.values_of("directories").unwrap().collect();
let schema_id = command_args.value_of("schema").unwrap();
let schema_type = command_args.value_of("type").unwrap();
let input_file = command_args.value_of("input").unwrap();
let value = fs::read(input_file).expect("Can not load given ion file");
let owned_elements = Element::read_all(value).expect("parsing failed unexpectedly");
let mut document_authorities: Vec<Box<dyn DocumentAuthority>> = vec![];
for authority in authorities {
document_authorities.push(Box::new(FileSystemDocumentAuthority::new(Path::new(
authority,
))))
}
let mut schema_system = SchemaSystem::new(document_authorities);
let schema = schema_system.load_schema(schema_id);
let type_ref = schema.unwrap().get_type(schema_type).unwrap();
let output = vec![];
let write_config = WriteConfig::<ion_rs::v1_0::Text>::new(TextFormat::Pretty);
let mut writer = Writer::new(write_config, output)?;
for owned_element in owned_elements {
let mut struct_writer = writer.struct_writer()?;
let validation_result = type_ref.validate(&owned_element);
match validation_result {
Ok(_) => {
struct_writer.field_writer("result").write_string("Valid")?;
struct_writer
.field_writer("value")
.write_string(format!("{owned_element}"))?;
struct_writer
.field_writer("schema")
.write_string(schema_id)?;
}
Err(_) => {
struct_writer
.field_writer("result")
.write_string("Invalid")?;
struct_writer
.field_writer("violation")
.write_string(format!("{:#?}", validation_result.unwrap_err()))?;
}
}
struct_writer.close()?;
}
let output = writer.close()?;
println!("Validation report:");
println!("{}", from_utf8(&output).unwrap());
Ok(())
}