Debian Codegraph @

src/specification/draft201909.rs

raw symbols SCIP
1 use serde_json::Value;
2
3 use crate::{resource::InnerResourcePtr, segments::Segment, Error, Resolver, Segments};
4
5 use super::subresources::SubresourceIteratorInner;
6
7 pub(crate) fn object_iter<'a>(
8 (key, value): (&'a String, &'a Value),
9 ) -> SubresourceIteratorInner<'a> {
10 match key.as_str() {
11 // For these keys, yield the value once.
12 "additionalItems"
13 | "additionalProperties"
14 | "contains"
15 | "contentSchema"
16 | "else"
17 | "if"
18 | "not"
19 | "propertyNames"
20 | "then"
21 | "unevaluatedItems"
22 | "unevaluatedProperties" => SubresourceIteratorInner::Once(value),
23 // For these keys, if the value is an array, iterate over its items.
24 "allOf" | "anyOf" | "oneOf" => {
25 if let Some(arr) = value.as_array() {
26 SubresourceIteratorInner::Array(arr.iter())
27 } else {
28 SubresourceIteratorInner::Empty
29 }
30 }
31 // For these keys, if the value is an object, iterate over its values.
32 "$defs" | "definitions" | "dependentSchemas" | "patternProperties" | "properties" => {
33 if let Some(obj) = value.as_object() {
34 SubresourceIteratorInner::Object(obj.values())
35 } else {
36 SubresourceIteratorInner::Empty
37 }
38 }
39 // For "items": if it's an array, iterate over its items; otherwise, yield the value once.
40 "items" => match value {
41 Value::Array(arr) => SubresourceIteratorInner::Array(arr.iter()),
42 _ => SubresourceIteratorInner::Once(value),
43 },
44 // For any other key, yield nothing.
45 _ => SubresourceIteratorInner::Empty,
46 }
47 }
48
49 pub(crate) fn maybe_in_subresource<'r>(
50 segments: &Segments,
51 resolver: &Resolver<'r>,
52 subresource: &InnerResourcePtr,
53 ) -> Result<Resolver<'r>, Error> {
54 const IN_VALUE: &[&str] = &[
55 "additionalItems",
56 "additionalProperties",
57 "contains",
58 "contentSchema",
59 "else",
60 "if",
61 "not",
62 "propertyNames",
63 "then",
64 "unevaluatedItems",
65 "unevaluatedProperties",
66 ];
67 const IN_CHILD: &[&str] = &[
68 "allOf",
69 "anyOf",
70 "oneOf",
71 "$defs",
72 "definitions",
73 "dependentSchemas",
74 "patternProperties",
75 "properties",
76 ];
77
78 let mut iter = segments.iter();
79 while let Some(segment) = iter.next() {
80 if let Segment::Key(key) = segment {
81 if *key == "items" && subresource.contents().is_object() {
82 return resolver.in_subresource_inner(subresource);
83 }
84 if !IN_VALUE.contains(&key.as_ref())
85 && (!IN_CHILD.contains(&key.as_ref()) || iter.next().is_none())
86 {
87 return Ok(resolver.clone());
88 }
89 }
90 }
91 resolver.in_subresource_inner(subresource)
92 }