@@ -899,27 +899,24 @@ impl ConstraintValidator for OrderedElementsConstraint {
899
899
) -> ValidationResult {
900
900
let violations: Vec < Violation > = vec ! [ ] ;
901
901
902
- let values: Vec < Element > = match & value {
903
- IonSchemaElement :: SingleElement ( element) => match element. as_sequence ( ) {
904
- None => {
905
- return Err ( Violation :: with_violations (
906
- "ordered_elements" ,
907
- ViolationCode :: TypeMismatched ,
908
- format ! (
909
- "expected list/sexp ion found {}" ,
910
- if element. is_null( ) {
911
- format!( "{element}" )
912
- } else {
913
- format!( "{}" , element. ion_type( ) )
914
- }
915
- ) ,
916
- ion_path,
917
- violations,
918
- ) ) ;
919
- }
920
- Some ( sequence) => sequence. elements ( ) . map ( |a| a. to_owned ( ) ) . collect ( ) ,
921
- } ,
922
- IonSchemaElement :: Document ( document) => document. to_owned ( ) ,
902
+ let mut element_iter = match value. as_sequence_iter ( ) {
903
+ Some ( iter) => iter. peekable ( ) ,
904
+ None => {
905
+ return Err ( Violation :: with_violations (
906
+ "ordered_elements" ,
907
+ ViolationCode :: TypeMismatched ,
908
+ format ! (
909
+ "expected list, sexp, or document; found {}" ,
910
+ if value. is_null( ) {
911
+ format!( "{value}" )
912
+ } else {
913
+ format!( "{}" , value. ion_schema_type( ) )
914
+ }
915
+ ) ,
916
+ ion_path,
917
+ violations,
918
+ ) ) ;
919
+ }
923
920
} ;
924
921
925
922
// build nfa for validation
@@ -928,7 +925,7 @@ impl ConstraintValidator for OrderedElementsConstraint {
928
925
type_store,
929
926
) ;
930
927
931
- if !values . is_empty ( ) && nfa_evaluation. nfa . get_final_states ( ) . is_empty ( ) {
928
+ if element_iter . peek ( ) . is_some ( ) && nfa_evaluation. nfa . get_final_states ( ) . is_empty ( ) {
932
929
return Err ( Violation :: with_violations (
933
930
"ordered_elements" ,
934
931
ViolationCode :: TypeMismatched ,
@@ -939,7 +936,7 @@ impl ConstraintValidator for OrderedElementsConstraint {
939
936
}
940
937
941
938
// use nfa_evaluation for validation
942
- nfa_evaluation. validate_ordered_elements ( & values , type_store) ;
939
+ nfa_evaluation. validate_ordered_elements ( element_iter , type_store) ;
943
940
944
941
if !nfa_evaluation. has_final_state ( type_store) {
945
942
return Err ( Violation :: with_violations (
@@ -1173,36 +1170,24 @@ impl ConstraintValidator for ContainsConstraint {
1173
1170
type_store : & TypeStore ,
1174
1171
ion_path : & mut IonPath ,
1175
1172
) -> ValidationResult {
1176
- // Create a peekable iterator for given sequence
1177
- let values: Vec < Element > = match & value {
1178
- IonSchemaElement :: SingleElement ( element) => {
1179
- match element. value ( ) {
1180
- Value :: List ( ion_sequence) | Value :: SExp ( ion_sequence) => {
1181
- ion_sequence. elements ( ) . map ( |a| a. to_owned ( ) ) . collect ( )
1182
- }
1183
- Value :: Struct ( ion_struct) => ion_struct
1184
- . fields ( )
1185
- . map ( |( name, value) | value. to_owned ( ) )
1186
- . collect ( ) ,
1187
- _ => {
1188
- // return Violation if value is not an Ion sequence
1189
- return Err ( Violation :: new (
1190
- "contains" ,
1191
- ViolationCode :: TypeMismatched ,
1192
- format ! (
1193
- "expected list/sexp/struct/document found {}" ,
1194
- if element. is_null( ) {
1195
- format!( "{element}" )
1196
- } else {
1197
- format!( "{}" , element. ion_type( ) )
1198
- }
1199
- ) ,
1200
- ion_path,
1201
- ) ) ;
1173
+ let values: Vec < IonData < & Element > > = if let Some ( element_iter) = value. as_sequence_iter ( ) {
1174
+ element_iter. map ( IonData :: from) . collect ( )
1175
+ } else if let Some ( strukt) = value. as_struct ( ) {
1176
+ strukt. fields ( ) . map ( |( k, v) | v) . map ( IonData :: from) . collect ( )
1177
+ } else {
1178
+ return Err ( Violation :: new (
1179
+ "contains" ,
1180
+ ViolationCode :: TypeMismatched ,
1181
+ format ! (
1182
+ "expected list/sexp/struct/document found {}" ,
1183
+ if value. is_null( ) {
1184
+ format!( "{value}" )
1185
+ } else {
1186
+ format!( "{}" , value. ion_schema_type( ) )
1202
1187
}
1203
- }
1204
- }
1205
- IonSchemaElement :: Document ( document ) => document . to_owned ( ) ,
1188
+ ) ,
1189
+ ion_path ,
1190
+ ) ) ;
1206
1191
} ;
1207
1192
1208
1193
// add all the missing values found during validation
@@ -1256,36 +1241,24 @@ impl ConstraintValidator for ContainerLengthConstraint {
1256
1241
ion_path : & mut IonPath ,
1257
1242
) -> ValidationResult {
1258
1243
// get the size of given value container
1259
- let size = match value {
1260
- IonSchemaElement :: SingleElement ( element) => {
1261
- // Check for null container
1262
- if element. is_null ( ) {
1263
- return Err ( Violation :: new (
1264
- "container_length" ,
1265
- ViolationCode :: TypeMismatched ,
1266
- format ! ( "expected a container found {element}" ) ,
1267
- ion_path,
1268
- ) ) ;
1269
- }
1270
-
1271
- match element. ion_type ( ) {
1272
- IonType :: List | IonType :: SExp => element. as_sequence ( ) . unwrap ( ) . len ( ) ,
1273
- IonType :: Struct => element. as_struct ( ) . unwrap ( ) . iter ( ) . count ( ) ,
1274
- _ => {
1275
- // return Violation if value is not an Ion container
1276
- return Err ( Violation :: new (
1277
- "container_length" ,
1278
- ViolationCode :: TypeMismatched ,
1279
- format ! (
1280
- "expected a container (a list/sexp/struct) but found {}" ,
1281
- element. ion_type( )
1282
- ) ,
1283
- ion_path,
1284
- ) ) ;
1285
- }
1286
- }
1287
- }
1288
- IonSchemaElement :: Document ( document) => document. len ( ) ,
1244
+ let size = if let Some ( element_iter) = value. as_sequence_iter ( ) {
1245
+ element_iter. count ( )
1246
+ } else if let Some ( strukt) = value. as_struct ( ) {
1247
+ strukt. fields ( ) . map ( |( k, v) | v) . count ( )
1248
+ } else {
1249
+ return Err ( Violation :: new (
1250
+ "container_length" ,
1251
+ ViolationCode :: TypeMismatched ,
1252
+ if value. is_null ( ) {
1253
+ format ! ( "expected a container found {value}" )
1254
+ } else {
1255
+ format ! (
1256
+ "expected a container (a list/sexp/struct) but found {}" ,
1257
+ value. ion_schema_type( )
1258
+ )
1259
+ } ,
1260
+ ion_path,
1261
+ ) ) ;
1289
1262
} ;
1290
1263
1291
1264
// get isl length as a range
@@ -1438,58 +1411,38 @@ impl ConstraintValidator for ElementConstraint {
1438
1411
let mut element_set = vec ! [ ] ;
1439
1412
1440
1413
// get elements for given container in the form (ion_path_element, element_value)
1441
- let elements: Vec < ( IonPathElement , & Element ) > = match value {
1442
- IonSchemaElement :: SingleElement ( element) => {
1414
+ let elements: Vec < ( IonPathElement , & Element ) > =
1415
+ if let Some ( element_iter) = value. as_sequence_iter ( ) {
1416
+ element_iter
1417
+ . enumerate ( )
1418
+ . map ( |( index, val) | ( IonPathElement :: Index ( index) , val) )
1419
+ . collect ( )
1420
+ } else if let Some ( strukt) = value. as_struct ( ) {
1421
+ strukt
1422
+ . fields ( )
1423
+ . map ( |( name, val) | ( IonPathElement :: Field ( name. to_string ( ) ) , val) )
1424
+ . collect ( )
1425
+ } else {
1443
1426
// Check for null container
1444
- if element . is_null ( ) {
1427
+ if value . is_null ( ) {
1445
1428
return Err ( Violation :: new (
1446
1429
"element" ,
1447
1430
ViolationCode :: TypeMismatched ,
1448
- format ! ( "expected a container but found {element }" ) ,
1431
+ format ! ( "expected a container but found {value }" ) ,
1449
1432
ion_path,
1450
1433
) ) ;
1451
1434
}
1452
-
1453
- // validate each element of the given value container
1454
- match element. ion_type ( ) {
1455
- IonType :: List | IonType :: SExp => element
1456
- . as_sequence ( )
1457
- . unwrap ( )
1458
- . elements ( )
1459
- . enumerate ( )
1460
- . map ( |( index, val) | ( IonPathElement :: Index ( index) , val) )
1461
- . collect ( ) ,
1462
- IonType :: Struct => element
1463
- . as_struct ( )
1464
- . unwrap ( )
1465
- . iter ( )
1466
- . map ( |( field_name, val) | {
1467
- (
1468
- IonPathElement :: Field ( field_name. text ( ) . unwrap ( ) . to_owned ( ) ) ,
1469
- val,
1470
- )
1471
- } )
1472
- . collect ( ) ,
1473
- _ => {
1474
- // return Violation if value is not an Ion container
1475
- return Err ( Violation :: new (
1476
- "element" ,
1477
- ViolationCode :: TypeMismatched ,
1478
- format ! (
1479
- "expected a container (a list/sexp/struct) but found {}" ,
1480
- element. ion_type( )
1481
- ) ,
1482
- ion_path,
1483
- ) ) ;
1484
- }
1485
- }
1486
- }
1487
- IonSchemaElement :: Document ( document) => document
1488
- . iter ( )
1489
- . enumerate ( )
1490
- . map ( |( index, val) | ( IonPathElement :: Index ( index) , val) )
1491
- . collect ( ) ,
1492
- } ;
1435
+ // return Violation if value is not an Ion container
1436
+ return Err ( Violation :: new (
1437
+ "element" ,
1438
+ ViolationCode :: TypeMismatched ,
1439
+ format ! (
1440
+ "expected a container (a list/sexp/struct) but found {}" ,
1441
+ value. ion_schema_type( )
1442
+ ) ,
1443
+ ion_path,
1444
+ ) ) ;
1445
+ } ;
1493
1446
1494
1447
// validate element constraint
1495
1448
for ( ion_path_element, val) in elements {
0 commit comments