| 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/::ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#, |
| 4 | extrust-analyzer cargo jsonschema 0.37.4 ext/::cmprust-analyzer cargo jsonschema 0.37.4 ext/cmp/, |
| 5 | keywordsrust-analyzer cargo jsonschema 0.37.4 keywords/::CompilationResultrust-analyzer cargo jsonschema 0.37.4 keywords/CompilationResult#, |
| 6 | 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#}, |
| 7 | typesrust-analyzer cargo jsonschema 0.37.4 types/::{JsonTyperust-analyzer cargo jsonschema 0.37.4 types/JsonType#, JsonTypeSetrust-analyzer cargo jsonschema 0.37.4 types/JsonTypeSet#}, |
| 8 | validatorrust-analyzer cargo jsonschema 0.37.4 validator/::{Validaterust-analyzer cargo jsonschema 0.37.4 validator/Validate#, ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#}, |
| 9 | }; |
| 10 | 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#}; |
| 11 | |
| 12 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 13 | pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) struct EnumValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator# { |
| 14 | optionsrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator#options.: Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, |
| 15 | // Types that occur in items |
| 16 | typesrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator#types.: JsonTypeSetrust-analyzer cargo jsonschema 0.37.4 types/JsonTypeSet#, |
| 17 | itemsrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator#items.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Valuerust-analyzer cargo serde_json 1.0.145 value/Value#>, |
| 18 | locationrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator#location.: Locationrust-analyzer cargo jsonschema 0.37.4 paths/Location#, |
| 19 | } |
| 20 | |
| 21 | impl EnumValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator# { |
| 22 | #[inline] |
| 23 | pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) fn compilerust-analyzer cargo jsonschema 0.37.4 keywords/enum_/impl#[EnumValidator]compile().<'a>( |
| 24 | schema: &'a Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, |
| 25 | items: &'a [Valuerust-analyzer cargo serde_json 1.0.145 value/Value#], |
| 26 | location: Locationrust-analyzer cargo jsonschema 0.37.4 paths/Location#, |
| 27 | ) -> CompilationResultrust-analyzer cargo jsonschema 0.37.4 keywords/CompilationResult#<'a> { |
| 28 | let mut types = JsonTypeSetrust-analyzer cargo jsonschema 0.37.4 types/JsonTypeSet#::emptyrust-analyzer cargo jsonschema 0.37.4 types/impl#[JsonTypeSet]empty().(); |
| 29 | for item in items { |
| 30 | types = types.insertrust-analyzer cargo jsonschema 0.37.4 types/impl#[JsonTypeSet]insert().(JsonTyperust-analyzer cargo jsonschema 0.37.4 types/JsonType#::fromrust-analyzer cargo jsonschema 0.37.4 types/impl#[JsonType][`From<&Value>`]from().(item)); |
| 31 | } |
| 32 | 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().(EnumValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator# { |
| 33 | optionsrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator#options.: schema.clonerust-analyzer cargo serde_json 1.0.145 value/impl#[Value][Clone]clone().(), |
| 34 | itemsrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator#items.: items.to_vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/impl#[`[T]`]to_vec().(), |
| 35 | typesrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator#types., |
| 36 | locationrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator#location., |
| 37 | })) |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | impl Validaterust-analyzer cargo jsonschema 0.37.4 validator/Validate# for EnumValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator# { |
| 42 | fn validaterust-analyzer cargo jsonschema 0.37.4 keywords/enum_/impl#[EnumValidator][Validate]validate().<'i>( |
| 43 | &self, |
| 44 | instance: &'i Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, |
| 45 | location: &LazyLocationrust-analyzer cargo jsonschema 0.37.4 paths/LazyLocation#, |
| 46 | ctx: &mut ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#, |
| 47 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#<'i>> { |
| 48 | if self.is_validrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/impl#[EnumValidator][Validate]is_valid().(instance, ctx) { |
| 49 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 50 | } else { |
| 51 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#::enumerationrust-analyzer cargo jsonschema 0.37.4 error/impl#[`ValidationError<'a>`]enumeration().( |
| 52 | self.locationrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator#location..clonerust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location][Clone]clone().(), |
| 53 | location.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 54 | instance, |
| 55 | &self.optionsrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator#options., |
| 56 | )) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | fn is_validrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/impl#[EnumValidator][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 { |
| 61 | // If the input value type is not in the types present among the enum options, then there |
| 62 | // is no reason to compare it against all items - we know that |
| 63 | // there are no items with such type at all |
| 64 | if self.typesrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator#types..contains_value_typerust-analyzer cargo jsonschema 0.37.4 types/impl#[JsonTypeSet]contains_value_type().(instance) { |
| 65 | self.itemsrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator#items..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().(|item| cmprust-analyzer cargo jsonschema 0.37.4 ext/cmp/::equalrust-analyzer cargo jsonschema 0.37.4 ext/cmp/equal().(instance, item)) |
| 66 | } else { |
| 67 | false |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 73 | pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) struct SingleValueEnumValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator# { |
| 74 | valuerust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator#value.: Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, |
| 75 | optionsrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator#options.: Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, |
| 76 | locationrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator#location.: Locationrust-analyzer cargo jsonschema 0.37.4 paths/Location#, |
| 77 | } |
| 78 | |
| 79 | impl SingleValueEnumValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator# { |
| 80 | #[inline] |
| 81 | pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) fn compilerust-analyzer cargo jsonschema 0.37.4 keywords/enum_/impl#[SingleValueEnumValidator]compile().<'a>( |
| 82 | schema: &'a Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, |
| 83 | value: &'a Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, |
| 84 | location: Locationrust-analyzer cargo jsonschema 0.37.4 paths/Location#, |
| 85 | ) -> CompilationResultrust-analyzer cargo jsonschema 0.37.4 keywords/CompilationResult#<'a> { |
| 86 | 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().(SingleValueEnumValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator# { |
| 87 | optionsrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator#options.: schema.clonerust-analyzer cargo serde_json 1.0.145 value/impl#[Value][Clone]clone().(), |
| 88 | valuerust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator#value.: value.clonerust-analyzer cargo serde_json 1.0.145 value/impl#[Value][Clone]clone().(), |
| 89 | locationrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator#location., |
| 90 | })) |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | impl Validaterust-analyzer cargo jsonschema 0.37.4 validator/Validate# for SingleValueEnumValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator# { |
| 95 | fn validaterust-analyzer cargo jsonschema 0.37.4 keywords/enum_/impl#[SingleValueEnumValidator][Validate]validate().<'i>( |
| 96 | &self, |
| 97 | instance: &'i Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, |
| 98 | location: &LazyLocationrust-analyzer cargo jsonschema 0.37.4 paths/LazyLocation#, |
| 99 | ctx: &mut ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#, |
| 100 | ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#<'i>> { |
| 101 | if self.is_validrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/impl#[SingleValueEnumValidator][Validate]is_valid().(instance, ctx) { |
| 102 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 103 | } else { |
| 104 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#::enumerationrust-analyzer cargo jsonschema 0.37.4 error/impl#[`ValidationError<'a>`]enumeration().( |
| 105 | self.locationrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator#location..clonerust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location][Clone]clone().(), |
| 106 | location.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(), |
| 107 | instance, |
| 108 | &self.optionsrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator#options., |
| 109 | )) |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | fn is_validrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/impl#[SingleValueEnumValidator][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 { |
| 114 | cmprust-analyzer cargo jsonschema 0.37.4 ext/cmp/::equalrust-analyzer cargo jsonschema 0.37.4 ext/cmp/equal().(&self.valuerust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator#value., instance) |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | #[inline] |
| 119 | pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) fn compilerust-analyzer cargo jsonschema 0.37.4 keywords/enum_/compile().<'a>( |
| 120 | ctx: &compilerrust-analyzer cargo jsonschema 0.37.4 compiler/::Contextrust-analyzer cargo jsonschema 0.37.4 compiler/Context#, |
| 121 | _: &'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#>, |
| 122 | schema: &'a Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, |
| 123 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CompilationResultrust-analyzer cargo jsonschema 0.37.4 keywords/CompilationResult#<'a>> { |
| 124 | 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 { |
| 125 | let location = ctx.locationrust-analyzer cargo jsonschema 0.37.4 compiler/impl#[`Context<'a>`]location().().joinrust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location]join().("enum"); |
| 126 | if items.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 cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().1 { |
| 127 | let value = items.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().nextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]next().().expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]expect().("Vec is not empty"); |
| 128 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(SingleValueEnumValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/SingleValueEnumValidator#::compilerust-analyzer cargo jsonschema 0.37.4 keywords/enum_/impl#[SingleValueEnumValidator]compile().(schema, value, location)) |
| 129 | } else { |
| 130 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(EnumValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/EnumValidator#::compilerust-analyzer cargo jsonschema 0.37.4 keywords/enum_/impl#[EnumValidator]compile().(schema, items, location)) |
| 131 | } |
| 132 | } else { |
| 133 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(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().( |
| 134 | Locationrust-analyzer cargo jsonschema 0.37.4 paths/Location#::newrust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location]new().(), |
| 135 | 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().(), |
| 136 | schema, |
| 137 | JsonTyperust-analyzer cargo jsonschema 0.37.4 types/JsonType#::Arrayrust-analyzer cargo jsonschema 0.37.4 types/JsonType#Array#, |
| 138 | ))) |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | #[cfg(test)] |
| 143 | mod testsrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/tests/ { |
| 144 | use craterust-analyzer cargo jsonschema 0.37.4 crate/::tests_utilrust-analyzer cargo jsonschema 0.37.4 tests_util/; |
| 145 | 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#}; |
| 146 | use test_caserust-analyzer cargo test-case 3.3.1 crate/::test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:; |
| 147 | |
| 148 | #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(&jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({"enum": [1]}), &jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!(2), "/enum")] |
| 149 | #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(&jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({"enum": [1, 3]}), &jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!(2), "/enum")] |
| 150 | fn locationrust-analyzer cargo jsonschema 0.37.4 keywords/enum_/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) { |
| 151 | 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); |
| 152 | } |
| 153 | } |