Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to [email protected] and remove re-export of ion-rs types #220

Merged
merged 3 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ schema_footer::{

### Loading a schema and validating an Ion value
```rust
use ion_rs::element::Element;
use ion_rs::Element;
use ion_schema::authority::{DocumentAuthority, FileSystemDocumentAuthority};
use ion_schema::result::{IonSchemaResult, ValidationResult};
use ion_schema::schema::Schema;
Expand Down
2 changes: 1 addition & 1 deletion ion-schema-tests-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ proc-macro = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ion-rs = "0.18.1"
ion-rs = "1.0.0-rc.7"
ion-schema = { path = "../ion-schema" }
quote = "1.0.21"
syn = "1.0.102"
Expand Down
11 changes: 6 additions & 5 deletions ion-schema-tests-runner/src/generator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::generator::util::*;
use crate::model::{TestCaseDetails, TestCaseVec};
use ion_rs::element::Element;
use ion_rs::Element;
use ion_rs::IonType;
use ion_rs::Sequence;
use ion_schema::isl::IslVersion;
use proc_macro2::{Literal, TokenStream, TokenTree};
use quote::{format_ident, quote};
Expand Down Expand Up @@ -121,7 +122,7 @@ fn generate_test_cases_for_file(ctx: Context) -> TokenStream {
// get the schema content from given schema file path
let ion_content = fs::read(ctx.current_dir.as_path())
.unwrap_or_else(|e| panic!("Unable to read {path_string} – {e}"));
let schema_content =
let schema_content: Sequence =
Element::read_all(ion_content).unwrap_or_else(|e| panic!("Error in {path_string} – {e:?}"));

let isl_version = find_isl_version(&schema_content);
Expand Down Expand Up @@ -173,7 +174,7 @@ fn generate_test_cases_for_file(ctx: Context) -> TokenStream {
}

/// find ISL version from schema content
fn find_isl_version(schema_content: &[Element]) -> IslVersion {
fn find_isl_version(schema_content: &Sequence) -> IslVersion {
// ISL version marker regex
let isl_version_marker: Regex = Regex::new(r"^\$ion_schema_\d.*$").unwrap();

Expand Down Expand Up @@ -266,7 +267,7 @@ fn generate_preamble(root_dir_path: &Path) -> TokenStream {
let root_dir_token = TokenTree::from(Literal::string(root_dir_path.to_str().unwrap()));

quote! {
use ion_rs::element::Sequence;
use ion_rs::Sequence;
use std::hash::{Hash, Hasher};

/// Gets the root directory for the test suite.
Expand Down Expand Up @@ -322,7 +323,7 @@ fn generate_preamble(root_dir_path: &Path) -> TokenStream {
fn __assert_value_validity_for_type(value_ion: &str, schema_id: &str, type_id: &str, expect_valid: bool) -> Result<(), String> {
let schema = __new_schema_system().load_schema(schema_id).unwrap();
let isl_type = schema.get_type(type_id).unwrap();
let value: ion_rs::element::Element = ion_rs::element::Element::read_one(value_ion.as_bytes()).unwrap();
let value: ion_rs::Element = ion_rs::Element::read_one(value_ion.as_bytes()).unwrap();
let prepared_value: ion_schema::IonSchemaElement = if value.annotations().contains("document") && value.ion_type() == ion_rs::IonType::SExp {
let element_vec = value.as_sequence()
.unwrap_or_else(|| unreachable!("We already confirmed that this is a s-expression."))
Expand Down
6 changes: 3 additions & 3 deletions ion-schema-tests-runner/src/model.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use ion_rs::element::{Element, List, Struct};
use ion_rs::*;
use std::ops::Deref;

// Represents a single test case.
Expand Down Expand Up @@ -211,9 +211,9 @@ impl StructUtils for Struct {
field_name,
Element::from(self.clone())
))?;
Ok(ion_rs::element::List(list.clone()))
Ok(ion_rs::List(list.clone()))
} else {
Ok(ion_rs::element::List(Element::sequence_builder().build()))
Ok(ion_rs::List(Element::sequence_builder().build()))
}
}
}
2 changes: 1 addition & 1 deletion ion-schema/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ion-rs = "0.18.1"
ion-rs = { version = "1.0.0-rc.7", features = ["experimental-reader-writer"] }
thiserror = "1.0"
num-bigint = "0.3"
num-traits = "0.2"
Expand Down
38 changes: 21 additions & 17 deletions ion-schema/examples/schema.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#[macro_use]
extern crate clap;
use clap::{App, ArgMatches};
use ion_rs::element::Element;
use ion_rs::IonWriter;
use ion_rs::{IonType, TextWriterBuilder};
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;
Expand Down Expand Up @@ -86,32 +85,37 @@ fn validate(command_args: &ArgMatches) -> IonSchemaResult<()> {
let type_ref = schema.unwrap().get_type(schema_type).unwrap();

// create a text writer to make the output
let mut output = vec![];
let mut writer = TextWriterBuilder::pretty().build(&mut output)?;
let output = vec![];
let write_config = WriteConfig::<ion_rs::v1_0::Text>::new(TextFormat::Pretty);
let mut writer = Writer::new(write_config, output)?;

// validate owned_elements according to type_ref
for owned_element in owned_elements {
// create a validation report with validation result, value, schema and/or violation
writer.step_in(IonType::Struct)?;
let mut struct_writer = writer.struct_writer()?;
let validation_result = type_ref.validate(&owned_element);
writer.set_field_name("result");
match validation_result {
Ok(_) => {
writer.write_string("Valid")?;
writer.set_field_name("value");
writer.write_string(format!("{owned_element}"))?;
writer.set_field_name("schema");
writer.write_string(schema_id)?;
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(_) => {
writer.write_string("Invalid")?;
writer.set_field_name("violation");
writer.write_string(format!("{:#?}", validation_result.unwrap_err()))?;
struct_writer
.field_writer("result")
.write_string("Invalid")?;
struct_writer
.field_writer("violation")
.write_string(format!("{:#?}", validation_result.unwrap_err()))?;
}
}
writer.step_out()?;
struct_writer.close()?;
}
drop(writer);
let output = writer.close()?;
println!("Validation report:");
println!("{}", from_utf8(&output).unwrap());
Ok(())
Expand Down
6 changes: 3 additions & 3 deletions ion-schema/src/authority.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
//! ```

use crate::result::{unresolvable_schema_error_raw, IonSchemaResult};
use ion_rs::element::Element;
use ion_rs::Element;
use std::collections::HashMap;
use std::fmt::Debug;
use std::fs;
Expand Down Expand Up @@ -108,7 +108,7 @@ impl DocumentAuthority for FileSystemDocumentAuthority {
// if absolute_path exists for the given id then load schema with file contents
let ion_content = fs::read(absolute_path)?;
let schema_content = Element::read_all(ion_content)?;
Ok(schema_content)
Ok(schema_content.into_iter().collect())
}
}

Expand Down Expand Up @@ -139,6 +139,6 @@ impl DocumentAuthority for MapDocumentAuthority {
))
})?;
let schema_content = Element::read_all(ion_content.as_bytes())?;
Ok(schema_content)
Ok(schema_content.into_iter().collect())
}
}
114 changes: 54 additions & 60 deletions ion-schema/src/constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use crate::type_reference::{TypeReference, VariablyOccurringTypeRef};
use crate::types::TypeValidator;
use crate::violation::{Violation, ViolationCode};
use crate::IonSchemaElement;
use ion_rs::element::Element;
use ion_rs::element::Value;
use ion_rs::Element;
use ion_rs::IonData;
use ion_rs::IonType;
use ion_rs::Value;
use num_traits::ToPrimitive;
use regex::{Regex, RegexBuilder};
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -814,7 +814,7 @@ impl OrderedElementsConstraint {
// * `min == 0`, that state will have a transition that advances to the next state automatically, making an occurrence of that `type_id` optional.
//
// Here is an example of how the built NFA would look like for an `ordered_elements` constraint:
// ```
// ```ion
// ordered_elements: [
// { type: int, occurs: optional },
// number,
Expand Down Expand Up @@ -1042,7 +1042,7 @@ impl ConstraintValidator for FieldsConstraint {
violations.push(Violation::new(
"fields",
ViolationCode::TypeMismatched,
&format!(
format!(
"Expected {} of field {}: found {}",
occurs_range,
field_name,
Expand Down Expand Up @@ -1116,7 +1116,8 @@ impl ConstraintValidator for FieldNamesConstraint {

for (field_name, _) in ion_struct.iter() {
ion_path.push(IonPathElement::Field(field_name.text().unwrap().to_owned()));
let schema_element: IonSchemaElement = (&Element::symbol(field_name)).into();
let field_name_symbol_as_element = Element::symbol(field_name);
let schema_element: IonSchemaElement = (&field_name_symbol_as_element).into();

if let Err(violation) =
self.type_reference
Expand Down Expand Up @@ -1188,7 +1189,7 @@ impl ConstraintValidator for ContainsConstraint {
return Err(Violation::new(
"contains",
ViolationCode::TypeMismatched,
&format!(
format!(
"expected list/sexp/struct/document found {}",
if element.is_null() {
format!("{element}")
Expand All @@ -1210,7 +1211,8 @@ impl ConstraintValidator for ContainsConstraint {
// for each value in expected values if it does not exist in ion sequence
// then add it to missing_values to keep track of missing values
for expected_value in self.values.iter() {
if !values.contains(expected_value) {
let expected = expected_value.into();
if !values.contains(&expected) {
missing_values.push(expected_value);
}
}
Expand Down Expand Up @@ -1547,36 +1549,31 @@ impl ConstraintValidator for AnnotationsConstraint2_0 {
type_store: &TypeStore,
ion_path: &mut IonPath,
) -> ValidationResult {
match value {
IonSchemaElement::SingleElement(element) => {
let schema_element: IonSchemaElement = (&element
.annotations()
.iter()
.map(Element::symbol)
.collect::<Vec<_>>())
.into();

self.type_ref
.validate(&schema_element, type_store, ion_path)
.map_err(|v| {
Violation::with_violations(
"annotations",
ViolationCode::AnnotationMismatched,
"one or more annotations don't satisfy annotations constraint",
ion_path,
vec![v],
)
})
}
IonSchemaElement::Document(document) => {
// document type can not have annotations
Err(Violation::new(
"annotations",
ViolationCode::AnnotationMismatched,
"annotations constraint is not applicable for document type",
ion_path,
))
}
if let Some(element) = value.as_element() {
let annotations: Vec<Element> =
element.annotations().iter().map(Element::symbol).collect();
let annotations_element: Element = ion_rs::Value::List(annotations.into()).into();
let annotations_ion_schema_element = IonSchemaElement::from(&annotations_element);

self.type_ref
.validate(&annotations_ion_schema_element, type_store, ion_path)
.map_err(|v| {
Violation::with_violations(
"annotations",
ViolationCode::AnnotationMismatched,
"one or more annotations don't satisfy annotations constraint",
ion_path,
vec![v],
)
})
} else {
// document type can not have annotations
Err(Violation::new(
"annotations",
ViolationCode::AnnotationMismatched,
"annotations constraint is not applicable for document type",
ion_path,
))
}
}
}
Expand Down Expand Up @@ -1748,26 +1745,23 @@ impl ConstraintValidator for AnnotationsConstraint {
) -> ValidationResult {
let violations: Vec<Violation> = vec![];

match value {
IonSchemaElement::SingleElement(element) => {
// validate annotations that have list-level `ordered` annotation
if self.is_ordered {
return self
.validate_ordered_annotations(element, type_store, violations, ion_path);
}

// validate annotations that does not have list-level `ordered` annotation
self.validate_unordered_annotations(element, type_store, violations, ion_path)
}
IonSchemaElement::Document(document) => {
// document type can not have annotations
Err(Violation::new(
"annotations",
ViolationCode::AnnotationMismatched,
"annotations constraint is not applicable for document type",
ion_path,
))
if let Some(element) = value.as_element() {
// validate annotations that have list-level `ordered` annotation
if self.is_ordered {
return self
.validate_ordered_annotations(element, type_store, violations, ion_path);
}

// validate annotations that does not have list-level `ordered` annotation
self.validate_unordered_annotations(element, type_store, violations, ion_path)
} else {
// document type can not have annotations
Err(Violation::new(
"annotations",
ViolationCode::AnnotationMismatched,
"annotations constraint is not applicable for document type",
ion_path,
))
}
}
}
Expand Down Expand Up @@ -1951,7 +1945,7 @@ impl ConstraintValidator for TimestampPrecisionConstraint {

// get isl timestamp precision as a range
let precision_range: &TimestampPrecisionRange = self.timestamp_precision();
let precision = &TimestampPrecision::from_timestamp(timestamp_value);
let precision = &TimestampPrecision::from_timestamp(&timestamp_value);
// return a Violation if the value didn't follow timestamp precision constraint
if !precision_range.contains(precision) {
return Err(Violation::new(
Expand Down Expand Up @@ -2000,8 +1994,8 @@ impl ConstraintValidator for ValidValuesConstraint {
type_store: &TypeStore,
ion_path: &mut IonPath,
) -> ValidationResult {
match value {
IonSchemaElement::SingleElement(element) => {
match value.as_element() {
Some(element) => {
for valid_value in &self.valid_values {
let does_match = match valid_value {
ValidValue::Element(valid_value) => {
Expand Down Expand Up @@ -2034,7 +2028,7 @@ impl ConstraintValidator for ValidValuesConstraint {
ion_path,
))
}
IonSchemaElement::Document(document) => Err(Violation::new(
_ => Err(Violation::new(
"valid_values",
ViolationCode::InvalidValue,
format!(
Expand Down
Loading
Loading