1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
use heck::MixedCase; use regex::Regex; use serde::de::Error; use serde_yaml::Mapping; use serde_yaml::Value; use crate::dsl::schema::deserialization::deserialize_schema; use crate::dsl::schema::object_types::bounds::ArrayItemObjectBounds; use crate::dsl::schema::object_types::bounds::ArrayObjectBounds; use crate::dsl::schema::object_types::bounds::ArrayUniqueItemBound; use crate::dsl::schema::object_types::bounds::DefaultValue; use crate::dsl::schema::object_types::bounds::enums::deserialize_enumeration; use crate::dsl::schema::object_types::bounds::IntegerBound; use crate::dsl::schema::object_types::bounds::IntegerObjectBounds; use crate::dsl::schema::object_types::bounds::StringLength; use crate::dsl::schema::object_types::bounds::StringObjectBounds; use crate::dsl::schema::object_types::deserialization::deserialize_integer; use crate::dsl::schema::Schema; use crate::dsl::schema::object_types::bounds::IntegerValueConditionObjectBounds; use crate::dsl::schema::object_types::bounds::BooleanObjectBounds; use crate::dsl::schema::object_types::bounds::StringValueObjectBounds; pub fn deserialize_string_object_bounds<E>(mapping: &Mapping) -> Result<Option<StringObjectBounds>, E> where E: Error, { let possible_values = deserialize_enumeration(&mapping)?; let pattern = deserialize_pattern(&mapping)?; let length = deserialize_length_bounds(&mapping)?; if possible_values.is_some() && pattern.is_some() { return Err(Error::custom("cannot have both pattern set and enum/const bound")); } if possible_values.is_some() && length.is_some() { return Err(Error::custom("cannot have both length set and enum/const bound")); } let result = { if let Some(values) = possible_values { Some(StringObjectBounds::Enumeration(values)) } else { Some(StringObjectBounds::Value(StringValueObjectBounds { pattern, length })) } }; Ok(result) } pub fn deserialize_boolean_object_bounds<E>(mapping: &Mapping) -> Result<Option<BooleanObjectBounds>, E> where E: Error, { Ok(deserialize_enumeration(&mapping)?.map(BooleanObjectBounds)) } pub fn deserialize_length_bounds<E>(mapping: &Mapping) -> Result<Option<StringLength>, E> where E: Error, { let max_length = deserialize_integer("maxLength", &mapping)?; let min_length = deserialize_integer("minLength", &mapping)?; if max_length.is_some() || min_length.is_some() { Ok(Some(StringLength { minimum: min_length, maximum: max_length, })) } else { Ok(None) } } pub fn deserialize_integer_bounds_with_defaults<E>( defaults: IntegerObjectBounds, mapping: &Mapping, ) -> Result<IntegerObjectBounds, E> where E: Error, { let bounds = deserialize_integer_bounds(mapping)?; match bounds { None => Ok(defaults), Some(bounds) => Ok(bounds.with_defaults(defaults)), } } pub fn deserialize_integer_bounds<E>(mapping: &Mapping) -> Result<Option<IntegerObjectBounds>, E> where E: Error, { let maximum = deserialize_integer_bound("max", mapping)?; let minimum = deserialize_integer_bound("min", mapping)?; let multiple_of = deserialize_integer("multipleOf", mapping)?; let possible_values = deserialize_enumeration(&mapping)?; let integer_bound_present = maximum.is_some() || minimum.is_some() || multiple_of.is_some(); if integer_bound_present && possible_values.is_some() { return Err(Error::custom( "cannot have both min/max/multiple bounds set and enum/const bound set at the same time", )); } if integer_bound_present { return Ok(Some(IntegerObjectBounds::Conditions( IntegerValueConditionObjectBounds { minimum, maximum, multiple_of, }, ))); } if let Some(values) = possible_values { return Ok(Some(IntegerObjectBounds::Enumeration(values))); } Ok(None) } pub fn deserialize_default_value<E>(mapping: &Mapping) -> Result<Option<DefaultValue>, E> where E: Error, { let default_key = Value::from("default"); let value = mapping.get(&default_key); let value = value.map(|value| DefaultValue(value.clone())); Ok(value) } pub fn deserialize_bool<E>(name: &str, mapping: &Mapping) -> Result<Option<bool>, E> where E: Error, { let default_key = Value::from(name); let value = mapping.get(&default_key); let value = value.and_then(|value| value.as_bool()); Ok(value) } pub fn deserialize_array_object_bounds<E>(mapping: &Mapping) -> Result<Option<ArrayObjectBounds>, E> where E: Error, { let maximum_number_of_items = deserialize_integer("maxItems", mapping)?; let minimum_number_of_items = deserialize_integer("minItems", mapping)?; let items = deserialize_array_item_bounds(mapping)?; let unique_items = deserialize_array_unique_items_bounds(mapping)?; if maximum_number_of_items.is_some() || minimum_number_of_items.is_some() || items.is_some() { return Ok(Some(ArrayObjectBounds { minimum_number_of_items, maximum_number_of_items, items, unique_items, })); } Ok(None) } fn deserialize_array_unique_items_bounds<E>(mapping: &Mapping) -> Result<Option<ArrayUniqueItemBound>, E> where E: Error, { match mapping.get(&Value::from("uniqueItems")) { None => Ok(None), Some(items) => match items { Value::Bool(value) => { if *value { Ok(Some(ArrayUniqueItemBound::All)) } else { Ok(None) } } Value::Sequence(sequence) => { let names = sequence .iter() .map(|item| match item { Value::String(name) => Ok(name.to_string()), _ => Err(Error::custom( "`uniqueItems` entry cannot be anything else than a string", )), }) .collect::<Result<Vec<String>, E>>(); Ok(Some(ArrayUniqueItemBound::Specific(names?))) } _ => Err(Error::custom(format!( "unsupported shape of the `uniqueItems` {:#?}", items ))), }, } } fn deserialize_array_item_bounds<E>(mapping: &Mapping) -> Result<Option<ArrayItemObjectBounds>, E> where E: Error, { match mapping.get(&Value::from("items")) { None => Ok(None), Some(properties) => match properties { Value::Mapping(_) => Ok(Some(ArrayItemObjectBounds::AllItems(Box::new(deserialize_schema( properties, )?)))), Value::Sequence(sequence) => Ok(Some(ArrayItemObjectBounds::AnyItems( sequence .iter() .map(|entry| deserialize_schema(entry)) .collect::<Result<Vec<Schema>, E>>()?, ))), _ => Err(Error::custom("`items` must be either a schema or array of schemas")), }, } } fn deserialize_pattern<E>(mapping: &Mapping) -> Result<Option<Regex>, E> where E: Error, { let pattern_key = Value::from("pattern"); match mapping.get(&pattern_key) { Some(pattern) => match pattern { Value::String(string) => { Ok(Some(Regex::new(string).map_err(|e| { Error::custom(format!("pattern `{:?}` is not a regex - {}", pattern, e)) })?)) } _ => Err(Error::custom(format!("pattern `{:#?}` must be a string", pattern))), }, None => Ok(None), } } fn deserialize_integer_bound<E>(name: &str, mapping: &Mapping) -> Result<Option<IntegerBound>, E> where E: Error, { let normal = deserialize_integer(name, mapping)?; let exclusive = deserialize_integer(&("exclusive ".to_string() + name).to_mixed_case(), mapping)?; if normal.is_some() && exclusive.is_some() { return Err(Error::custom("cannot have both {} and exclusive {} set")); } if let Some(normal) = normal { return Ok(Some(IntegerBound::Inclusive(normal))); } if let Some(exclusive) = exclusive { return Ok(Some(IntegerBound::Exclusive(exclusive))); } Ok(None) }