| 1 | use serde_json::Value; |
| 2 | |
| 3 | use crate::{resource::InnerResourcePtr, Error, Resolver, Segments}; |
| 4 | |
| 5 | use super::subresources::{self, 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 | | "else" |
| 16 | | "if" |
| 17 | | "not" |
| 18 | | "propertyNames" |
| 19 | | "then" => SubresourceIteratorInner::Once(value), |
| 20 | // For these keys, if the value is an array, iterate over its items. |
| 21 | "allOf" | "anyOf" | "oneOf" => { |
| 22 | if let Some(arr) = value.as_array() { |
| 23 | // In the old draft, flatten() was used. |
| 24 | // Here we simply iterate over the array. |
| 25 | SubresourceIteratorInner::Array(arr.iter()) |
| 26 | } else { |
| 27 | SubresourceIteratorInner::Empty |
| 28 | } |
| 29 | } |
| 30 | // For these keys, if the value is an object, iterate over its values. |
| 31 | "definitions" | "patternProperties" | "properties" => { |
| 32 | if let Some(obj) = value.as_object() { |
| 33 | // flat_map in the old draft: iterate over the object's values. |
| 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 "dependencies": if the value is an object, iterate over its values filtered to only those that are objects. |
| 45 | "dependencies" => { |
| 46 | if let Some(obj) = value.as_object() { |
| 47 | SubresourceIteratorInner::FilteredObject(obj.values()) |
| 48 | } else { |
| 49 | SubresourceIteratorInner::Empty |
| 50 | } |
| 51 | } |
| 52 | // For any other key, yield nothing. |
| 53 | _ => SubresourceIteratorInner::Empty, |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | pub(crate) fn maybe_in_subresource<'r>( |
| 58 | segments: &Segments, |
| 59 | resolver: &Resolver<'r>, |
| 60 | subresource: &InnerResourcePtr, |
| 61 | ) -> Result<Resolver<'r>, Error> { |
| 62 | const IN_VALUE: &[&str] = &[ |
| 63 | "additionalItems", |
| 64 | "additionalProperties", |
| 65 | "contains", |
| 66 | "else", |
| 67 | "if", |
| 68 | "not", |
| 69 | "propertyNames", |
| 70 | "then", |
| 71 | ]; |
| 72 | const IN_CHILD: &[&str] = &[ |
| 73 | "allOf", |
| 74 | "anyOf", |
| 75 | "oneOf", |
| 76 | "definitions", |
| 77 | "patternProperties", |
| 78 | "properties", |
| 79 | ]; |
| 80 | subresources::maybe_in_subresource_with_items_and_dependencies( |
| 81 | segments, |
| 82 | resolver, |
| 83 | subresource, |
| 84 | IN_VALUE, |
| 85 | IN_CHILD, |
| 86 | ) |
| 87 | } |