Debian Codegraph @

src/keywords/one_of.rs

raw symbols SCIP
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 evaluationrust-analyzer cargo jsonschema 0.37.4 evaluation/::ErrorDescriptionrust-analyzer cargo jsonschema 0.37.4 evaluation/ErrorDescription#,
5 keywordsrust-analyzer cargo jsonschema 0.37.4 keywords/::CompilationResultrust-analyzer cargo jsonschema 0.37.4 keywords/CompilationResult#,
6 noderust-analyzer cargo jsonschema 0.37.4 node/::SchemaNoderust-analyzer cargo jsonschema 0.37.4 node/SchemaNode#,
7 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#},
8 typesrust-analyzer cargo jsonschema 0.37.4 types/::JsonTyperust-analyzer cargo jsonschema 0.37.4 types/JsonType#,
9 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#},
10 };
11 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#};
12
13 pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) struct OneOfValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator# {
14 schemasrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator#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/one_of/OneOfValidator#location.: Locationrust-analyzer cargo jsonschema 0.37.4 paths/Location#,
16 }
17
18 impl OneOfValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator# {
19 #[inline]
20 pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) fn compilerust-analyzer cargo jsonschema 0.37.4 keywords/one_of/impl#[OneOfValidator]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().("oneOf");
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().(OneOfValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator# {
30 schemasrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator#schemas.,
31 locationrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator#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 fn get_first_validrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/impl#[OneOfValidator]get_first_valid().(&self, instance: &Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, ctx: &mut ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<usize> {
44 let mut first_valid_idx = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
45 for (idx, node) in self.schemasrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator#schemas..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().() {
46 if node.is_validrust-analyzer cargo jsonschema 0.37.4 node/impl#[SchemaNode][Validate]is_valid().(instance, ctx) {
47 first_valid_idx = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(idx);
48 break;
49 }
50 }
51 first_valid_idx
52 }
53
54 #[allow(clippy::arithmetic_side_effects)]
55 fn are_others_validrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/impl#[OneOfValidator]are_others_valid().(&self, instance: &Valuerust-analyzer cargo serde_json 1.0.145 value/Value#, idx: usize, ctx: &mut ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#) -> bool {
56 self.schemasrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator#schemas.
57 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
58 .skiprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#skip().(idx 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)
59 .anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#any().(|n| n.is_validrust-analyzer cargo jsonschema 0.37.4 node/impl#[SchemaNode][Validate]is_valid().(instance, ctx))
60 }
61 }
62
63 impl Validaterust-analyzer cargo jsonschema 0.37.4 validator/Validate# for OneOfValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator# {
64 fn is_validrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/impl#[OneOfValidator][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 {
65 let first_valid_idx = self.get_first_validrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/impl#[OneOfValidator]get_first_valid().(instance, ctx);
66 first_valid_idx.is_some_andrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some_and().(|idx| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().self.are_others_validrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/impl#[OneOfValidator]are_others_valid().(instance, idx, ctx))
67 }
68
69 fn validaterust-analyzer cargo jsonschema 0.37.4 keywords/one_of/impl#[OneOfValidator][Validate]validate().<'i>(
70 &self,
71 instance: &'i Valuerust-analyzer cargo serde_json 1.0.145 value/Value#,
72 location: &LazyLocationrust-analyzer cargo jsonschema 0.37.4 paths/LazyLocation#,
73 ctx: &mut ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#,
74 ) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#<'i>> {
75 let first_valid_idx = self.get_first_validrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/impl#[OneOfValidator]get_first_valid().(instance, ctx);
76 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(idx) = first_valid_idx {
77 if self.are_others_validrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/impl#[OneOfValidator]are_others_valid().(instance, idx, ctx) {
78 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#::one_of_multiple_validrust-analyzer cargo jsonschema 0.37.4 error/impl#[`ValidationError<'a>`]one_of_multiple_valid().(
79 self.locationrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator#location..clonerust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location][Clone]clone().(),
80 location.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
81 instance,
82 self.schemasrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator#schemas.
83 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
84 .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().())
85 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(),
86 ));
87 }
88 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
89 } else {
90 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(ValidationErrorrust-analyzer cargo jsonschema 0.37.4 error/ValidationError#::one_of_not_validrust-analyzer cargo jsonschema 0.37.4 error/impl#[`ValidationError<'a>`]one_of_not_valid().(
91 self.locationrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator#location..clonerust-analyzer cargo jsonschema 0.37.4 paths/impl#[Location][Clone]clone().(),
92 location.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().(),
93 instance,
94 self.schemasrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator#schemas.
95 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
96 .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().())
97 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().(),
98 ))
99 }
100 }
101
102 fn evaluaterust-analyzer cargo jsonschema 0.37.4 keywords/one_of/impl#[OneOfValidator][Validate]evaluate().(
103 &self,
104 instance: &Valuerust-analyzer cargo serde_json 1.0.145 value/Value#,
105 location: &LazyLocationrust-analyzer cargo jsonschema 0.37.4 paths/LazyLocation#,
106 ctx: &mut ValidationContextrust-analyzer cargo jsonschema 0.37.4 validator/ValidationContext#,
107 ) -> EvaluationResultrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult# {
108 let total = self.schemasrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator#schemas..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().();
109 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);
110 let mut iter = self.schemasrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator#schemas..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().();
111 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().() {
112 let child = node.evaluate_instancerust-analyzer cargo jsonschema 0.37.4 node/impl#[SchemaNode]evaluate_instance().(instance, location, ctx);
113 if child.validrust-analyzer cargo jsonschema 0.37.4 evaluation/EvaluationNode#valid. {
114 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().(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().()));
115 successes.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(child);
116 for node in iter {
117 let next = node.evaluate_instancerust-analyzer cargo jsonschema 0.37.4 node/impl#[SchemaNode]evaluate_instance().(instance, location, ctx);
118 if next.validrust-analyzer cargo jsonschema 0.37.4 evaluation/EvaluationNode#valid. {
119 successes.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(next);
120 }
121 }
122 return match successes.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() {
123 1 => EvaluationResultrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#::fromrust-analyzer cargo jsonschema 0.37.4 validator/impl#[EvaluationResult][`From<EvaluationNode>`]from().(successes.removerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]remove().(0)),
124 _ => EvaluationResultrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#::Invalidrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#Invalid# {
125 errorsrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#Invalid#errors.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![ErrorDescriptionrust-analyzer cargo jsonschema 0.37.4 evaluation/ErrorDescription#::newrust-analyzer cargo jsonschema 0.37.4 evaluation/impl#[ErrorDescription]new().(
126 "oneOf",
127 "more than one subschema succeeded".to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().(),
128 )],
129 childrenrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#Invalid#children.: successes,
130 annotationsrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#Invalid#annotations.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
131 },
132 };
133 }
134 failures.pushrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]push().(child);
135 }
136 EvaluationResultrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#::Invalidrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#Invalid# {
137 errorsrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#Invalid#errors.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T>`]new().(),
138 childrenrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#Invalid#children.: failures,
139 annotationsrust-analyzer cargo jsonschema 0.37.4 validator/EvaluationResult#Invalid#annotations.: Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#,
140 }
141 }
142 }
143
144 #[inline]
145 pub(craterust-analyzer cargo jsonschema 0.37.4 crate/) fn compilerust-analyzer cargo jsonschema 0.37.4 keywords/one_of/compile().<'a>(
146 ctx: &compilerrust-analyzer cargo jsonschema 0.37.4 compiler/::Contextrust-analyzer cargo jsonschema 0.37.4 compiler/Context#,
147 _: &'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#>,
148 schema: &'a Valuerust-analyzer cargo serde_json 1.0.145 value/Value#,
149 ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<CompilationResultrust-analyzer cargo jsonschema 0.37.4 keywords/CompilationResult#<'a>> {
150 Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(OneOfValidatorrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/OneOfValidator#::compilerust-analyzer cargo jsonschema 0.37.4 keywords/one_of/impl#[OneOfValidator]compile().(ctx, schema))
151 }
152
153 #[cfg(test)]
154 mod testsrust-analyzer cargo jsonschema 0.37.4 keywords/one_of/tests/ {
155 use craterust-analyzer cargo jsonschema 0.37.4 crate/::tests_utilrust-analyzer cargo jsonschema 0.37.4 tests_util/;
156 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#};
157 use test_caserust-analyzer cargo test-case 3.3.1 crate/::test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:;
158
159 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(&jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({"oneOf": [{"type": "string"}]}), &jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!(0), "/oneOf")]
160 #[test_caserust-analyzer cargo test-case-macros 3.3.1 test_case:(&jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!({"oneOf": [{"type": "string"}, {"maxLength": 3}]}), &jsonrust-analyzer cargo serde_json 1.0.145 macros/json!!(""), "/oneOf")]
161 fn locationrust-analyzer cargo jsonschema 0.37.4 keywords/one_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) {
162 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);
163 }
164 }