| 1 | use craterust-analyzer cargo jsonschema 0.37.4 crate/::{ |
| 2 | compilerrust-analyzer cargo jsonschema 0.37.4 compiler/, |
| 3 | errorrust-analyzer cargo jsonschema 0.37.4 error/::{errorrust-analyzer cargo jsonschema 0.37.4 error/error()., no_errorrust-analyzer cargo jsonschema 0.37.4 error/no_error()., ErrorIteratorrust-analyzer cargo jsonschema 0.37.4 error/ErrorIterator#, ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#}, |
| 4 | noderust-analyzer cargo jsonschema 0.37.4 node/::SchemaNoderust-analyzer cargo jsonschema 0.37.4 node/SchemaNode#, |
| 5 | pathsrust-analyzer cargo jsonschema 0.37.4 paths/::{LazyLocationrust-analyzer cargo jsonschema 0.37.4 paths/LazyLocation#, Locationrust-analyzer cargo jsonschema 0.37.4 paths/Location#}, |
| 6 | typesrust-analyzer cargo jsonschema 0.37.4 types/::JsonTyperust-analyzer cargo jsonschema 0.37.4 types/JsonType#, |
| 7 | validatorrust-analyzer cargo jsonschema 0.37.4 validator/::{EvaluationResultrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#, Validaterust-analyzer cargo jsonschema 0.37.4 validator/Validate#, ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#}, |
| 8 | }; |
| 9 | use serde_jsonrust-analyzer cargo serde_json 1.0.145 crate/::{Maprust-analyzer cargo serde_json 1.0.145 map/Map#, Valuerust-analyzer cargo serde_json 1.0.145 value/Value#}; |
| 10 | |
| 11 | use superrust-analyzer cargo jsonschema 0.37.4 keywords/::CompilationResultrust-analyzer cargo jsonschema 0.37.4 keywords/CompilationResult#; |
| 12 | |
| 13 | pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) struct AnyOfValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator# { |
| 14 | schemasrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator#schemas.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<SchemaNoderust-analyzer cargo jsonschema 0.37.4 node/SchemaNode#>, |
| 15 | locationrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator#location.: Locationrust-analyzer cargo jsonschema 0.37.4 paths/Location#, |
| 16 | } |
| 17 | |
| 18 | impl AnyOfValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator# { |
| 19 | #[inline] |
| 20 | pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) fn compilerust-analyzer cargo jsonschema 0.37.4 keywords/any_of/impl#[AnyOfValidator]compile().<'a>(ctx: &compilerrust-analyzer cargo jsonschema 0.37.4 compiler/::Contextrust-analyzer cargo jsonschema 0.37.4 compiler/Context#, schema: &'a Valuerust-analyzer cargo serde_json 1.0.145 value/Value#) -> CompilationResultrust-analyzer cargo jsonschema 0.37.4 keywords/CompilationResult#<'a> { |
| 21 | if let Valuerust-analyzer cargo serde_json 1.0.145 value/Value#::Arrayrust-analyzer cargo serde_json 1.0.145 value/Value#Array#(items) = schema { |
| 22 | let ctx = ctx.new_at_locationrust-analyzer cargo jsonschema 0.37.4 compiler/impl#[`Context<'a>`]new_at_location().("anyOf"); |
| 23 | let mut schemas = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(items.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 24 | for (idx, item) in items.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().enumeraterust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#enumerate().() { |
| 25 | let ctx = ctx.new_at_locationrust-analyzer cargo jsonschema 0.37.4 compiler/impl#[`Context<'a>`]new_at_location().(idx); |
| 26 | let node = compilerrust-analyzer cargo jsonschema 0.37.4 compiler/::compilerust-analyzer cargo jsonschema 0.37.4 compiler/compile().(&ctx, ctx.as_resource_refrust-analyzer cargo jsonschema 0.37.4 compiler/impl#[`Context<'a>`]as_resource_ref().(item))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 27 | schemas.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(node); |
| 28 | } |
| 29 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/impl#[`Box<T>`]new().(AnyOfValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator# { |
| 30 | schemasrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator#schemas., |
| 31 | locationrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator#location.: ctx.locationrust-analyzer cargo jsonschema 0.37.4 compiler/impl#[`Context<'a>`]location().().clonerust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location][Clone]clone().(), |
| 32 | })) |
| 33 | } else { |
| 34 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#::single_type_errorrust-analyzer cargo jsonschema 0.37.4 error/impl#[`ValidationError<'a>`]single_type_error().( |
| 35 | Locationrust-analyzer cargo jsonschema 0.37.4 paths/Location#::newrust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location]new().(), |
| 36 | ctx.locationrust-analyzer cargo jsonschema 0.37.4 compiler/impl#[`Context<'a>`]location().().clonerust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location][Clone]clone().(), |
| 37 | schema, |
| 38 | JsonTyperust-analyzer cargo jsonschema 0.37.4 types/JsonType#::Arrayrust-analyzer cargo jsonschema 0.37.4 types/JsonType#Array#, |
| 39 | )) |
| 40 | } |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl Validaterust-analyzer cargo jsonschema 0.37.4 validator/Validate# for AnyOfValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator# { |
| 45 | fn is_validrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/impl#[AnyOfValidator][Validate]is_valid().(&self, instance: &Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, ctx: &mut ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#) -> bool { |
| 46 | self.schemasrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator#schemas..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]any().(|s| s.is_validrust-analyzer cargo jsonschema 0.37.4 node/impl#[SchemaNode][Validate]is_valid().(instance, ctx)) |
| 47 | } |
| 48 | |
| 49 | fn validaterust-analyzer cargo jsonschema 0.37.4 keywords/any_of/impl#[AnyOfValidator][Validate]validate().<'i>( |
| 50 | &self, |
| 51 | instance: &'i Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, |
| 52 | location: &LazyLocationrust-analyzer cargo jsonschema 0.37.4 paths/LazyLocation#, |
| 53 | ctx: &mut ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#, |
| 54 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#<'i>> { |
| 55 | if self.is_validrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/impl#[AnyOfValidator][Validate]is_valid().(instance, ctx) { |
| 56 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 57 | } else { |
| 58 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#::any_ofrust-analyzer cargo jsonschema 0.37.4 error/impl#[`ValidationError<'a>`]any_of().( |
| 59 | self.locationrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator#location..clonerust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location][Clone]clone().(), |
| 60 | location.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 61 | instance, |
| 62 | self.schemasrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator#schemas. |
| 63 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 64 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|schema| schema.iter_errorsrust-analyzer cargo jsonschema 0.37.4 node/impl#[SchemaNode][Validate]iter_errors().(instance, location, ctx).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()) |
| 65 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 66 | )) |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | fn iter_errorsrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/impl#[AnyOfValidator][Validate]iter_errors().<'i>( |
| 71 | &self, |
| 72 | instance: &'i Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, |
| 73 | location: &LazyLocationrust-analyzer cargo jsonschema 0.37.4 paths/LazyLocation#, |
| 74 | ctx: &mut ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#, |
| 75 | ) -> ErrorIteratorrust-analyzer cargo jsonschema 0.37.4 error/ErrorIterator#<'i> { |
| 76 | if self.is_validrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/impl#[AnyOfValidator][Validate]is_valid().(instance, ctx) { |
| 77 | no_errorrust-analyzer cargo jsonschema 0.37.4 error/no_error().() |
| 78 | } else { |
| 79 | errorrust-analyzer cargo jsonschema 0.37.4 error/error().(ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#::any_ofrust-analyzer cargo jsonschema 0.37.4 error/impl#[`ValidationError<'a>`]any_of().( |
| 80 | self.locationrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator#location..clonerust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location][Clone]clone().(), |
| 81 | location.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 82 | instance, |
| 83 | self.schemasrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator#schemas. |
| 84 | .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() |
| 85 | .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|schema| schema.iter_errorsrust-analyzer cargo jsonschema 0.37.4 node/impl#[SchemaNode][Validate]iter_errors().(instance, location, ctx).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()) |
| 86 | .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(), |
| 87 | )) |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | fn evaluaterust-analyzer cargo jsonschema 0.37.4 keywords/any_of/impl#[AnyOfValidator][Validate]evaluate().( |
| 92 | &self, |
| 93 | instance: &Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, |
| 94 | location: &LazyLocationrust-analyzer cargo jsonschema 0.37.4 paths/LazyLocation#, |
| 95 | ctx: &mut ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#, |
| 96 | ) -> EvaluationResultrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult# { |
| 97 | let total = self.schemasrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator#schemas..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(); |
| 98 | let mut failures = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(total); |
| 99 | let mut iter = self.schemasrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator#schemas..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().(); |
| 100 | while let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(node) = iter.nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]next().() { |
| 101 | let result = node.evaluate_instancerust-analyzer cargo jsonschema 0.37.4 node/impl#[SchemaNode]evaluate_instance().(instance, location, ctx); |
| 102 | if result.validrust-analyzer cargo jsonschema 0.37.4 evaluation/EvaluationNode#valid. { |
| 103 | let remaining = total.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(failures.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1); |
| 104 | let mut successes = Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]with_capacity().(remaining rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().1); |
| 105 | successes.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(result); |
| 106 | for node in iter { |
| 107 | let tail = node.evaluate_instancerust-analyzer cargo jsonschema 0.37.4 node/impl#[SchemaNode]evaluate_instance().(instance, location, ctx); |
| 108 | if tail.validrust-analyzer cargo jsonschema 0.37.4 evaluation/EvaluationNode#valid. { |
| 109 | successes.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(tail); |
| 110 | } |
| 111 | } |
| 112 | return EvaluationResultrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#::from_childrenrust-analyzer cargo jsonschema 0.37.4 validator/impl#[EvaluationResult]from_children().(successes); |
| 113 | } |
| 114 | failures.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(result); |
| 115 | } |
| 116 | EvaluationResultrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#::from_childrenrust-analyzer cargo jsonschema 0.37.4 validator/impl#[EvaluationResult]from_children().(failures) |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #[inline] |
| 121 | pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) fn compilerust-analyzer cargo jsonschema 0.37.4 keywords/any_of/compile().<'a>( |
| 122 | ctx: &compilerrust-analyzer cargo jsonschema 0.37.4 compiler/::Contextrust-analyzer cargo jsonschema 0.37.4 compiler/Context#, |
| 123 | _: &'a Maprust-analyzer cargo serde_json 1.0.145 map/Map#<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, Valuerust-analyzer cargo serde_json 1.0.145 value/Value#>, |
| 124 | schema: &'a Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, |
| 125 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CompilationResultrust-analyzer cargo jsonschema 0.37.4 keywords/CompilationResult#<'a>> { |
| 126 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(AnyOfValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/AnyOfValidator#::compilerust-analyzer cargo jsonschema 0.37.4 keywords/any_of/impl#[AnyOfValidator]compile().(ctx, schema)) |
| 127 | } |
| 128 | |
| 129 | #[cfg(test)] |
| 130 | mod testsrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/tests/ { |
| 131 | use craterust-analyzer cargo jsonschema 0.37.4 crate/::tests_utilrust-analyzer cargo jsonschema 0.37.4 tests_util/; |
| 132 | use serde_jsonrust-analyzer cargo serde_json 1.0.145 crate/::{jsonrust-analyzer cargo serde_json 1.0.145 macros/json!, Valuerust-analyzer cargo serde_json 1.0.145 value/Value#}; |
| 133 | use test_caserust-analyzer cargo test-case 3.3.1 crate/::test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:; |
| 134 | |
| 135 | #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(&jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({"anyOf": [{"type": "string"}]}), &jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!(1), "/anyOf")] |
| 136 | #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(&jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({"anyOf": [{"type": "integer"}, {"type": "string"}]}), &jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({}), "/anyOf")] |
| 137 | fn locationrust-analyzer cargo jsonschema 0.37.4 keywords/any_of/tests/location().(schema: &Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, instance: &Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, expected: &str) { |
| 138 | tests_utilrust-analyzer cargo jsonschema 0.37.4 tests_util/::assert_schema_locationrust-analyzer cargo jsonschema 0.37.4 tests_util/assert_schema_location().(schema, instance, expected); |
| 139 | } |
| 140 | } |