From 097a40fcb51971790081722c8ce8c1c1209fe26b Mon Sep 17 00:00:00 2001 From: anweiss <2326106+anweiss@users.noreply.github.com> Date: Mon, 3 Oct 2022 11:41:42 -0400 Subject: [PATCH 1/3] fix #139 in json validator --- src/validator/json.rs | 83 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 17 deletions(-) diff --git a/src/validator/json.rs b/src/validator/json.rs index 12d04262..833828d3 100644 --- a/src/validator/json.rs +++ b/src/validator/json.rs @@ -1897,8 +1897,38 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { Ok(()) } Value::Array(_) => self.validate_array_items(&ArrayItemToken::Identifier(ident)), - Value::Object(o) => { - if let Some(occur) = &self.occurrence { + Value::Object(o) => match &self.occurrence { + #[cfg(feature = "ast-span")] + Some(Occur::Optional(_)) | None => { + if token::lookup_ident(ident.ident) + .in_standard_prelude() + .is_some() + { + self.add_error(format!( + "expected object value of type {}, got object", + ident.ident + )); + return Ok(()); + } + + self.visit_value(&token::Value::TEXT(ident.ident.into())) + } + #[cfg(not(feature = "ast-span"))] + Some(Occur::Optional) | None => { + if token::lookup_ident(ident.ident) + .in_standard_prelude() + .is_some() + { + self.add_error(format!( + "expected object value of type {}, got object", + ident.ident + )); + return Ok(()); + } + + self.visit_value(&token::Value::TEXT(ident.ident.into())) + } + Some(occur) => { if is_ident_string_data_type(self.cddl, ident) { let values_to_validate = o .iter() @@ -2026,22 +2056,9 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> { } } - return Ok(()); - } - - if token::lookup_ident(ident.ident) - .in_standard_prelude() - .is_some() - { - self.add_error(format!( - "expected object value of type {}, got object", - ident.ident - )); - return Ok(()); + Ok(()) } - - self.visit_value(&token::Value::TEXT(ident.ident.into())) - } + }, _ => { if let Some(cut_value) = self.cut_value.take() { self.add_error(format!( @@ -2727,4 +2744,36 @@ mod tests { Ok(()) } + + #[test] + fn validate_optional_occurrences_in_object() -> std::result::Result<(), Box> + { + let cddl = indoc!( + r#" + argument = { + name: text, + ? valid: "yes" / "no", + } + "# + ); + + let json = r#"{ + "name": "foo", + "valid": "no" + }"#; + + let cddl = cddl_from_str(cddl, true).map_err(json::Error::CDDLParsing); + if let Err(e) = &cddl { + println!("{}", e); + } + + let json = serde_json::from_str::(json).map_err(json::Error::JSONParsing)?; + + let cddl = cddl.unwrap(); + + let mut jv = JSONValidator::new(&cddl, json, None); + jv.validate().unwrap(); + + Ok(()) + } } From 6fc5935631991355db36ae0cf6e2333f10549053 Mon Sep 17 00:00:00 2001 From: anweiss <2326106+anweiss@users.noreply.github.com> Date: Mon, 3 Oct 2022 11:58:44 -0400 Subject: [PATCH 2/3] fix #139 in cbor validator --- src/validator/cbor.rs | 824 ++++++++++++++++++++++++++---------------- 1 file changed, 513 insertions(+), 311 deletions(-) diff --git a/src/validator/cbor.rs b/src/validator/cbor.rs index 35901050..ef0f565e 100644 --- a/src/validator/cbor.rs +++ b/src/validator/cbor.rs @@ -2295,397 +2295,599 @@ where } Value::Array(_) => self.validate_array_items(&ArrayItemToken::Identifier(ident)), Value::Map(m) => { - if let Some(occur) = &self.occurrence { - let mut errors = Vec::new(); - - if is_ident_string_data_type(self.cddl, ident) { - let values_to_validate = m - .iter() - .filter_map(|(k, v)| { - if let Some(keys) = &self.validated_keys { - if !keys.contains(k) { - if matches!(k, Value::Text(_)) { - Some(v.clone()) + match &self.occurrence { + #[cfg(feature = "ast-span")] + Some(Occur::Optional(_)) | None => { + if is_ident_string_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Text(_))) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + + return Ok(()); + } + + if is_ident_integer_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Integer(_))) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } + + if is_ident_bool_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Bool(_))) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } + + if is_ident_null_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Null)) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } + + if is_ident_byte_string_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Bytes(_))) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } + + if is_ident_float_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Null)) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } + + if token::lookup_ident(ident.ident) + .in_standard_prelude() + .is_some() + { + self.add_error(format!( + "expected object value of type {}, got object", + ident.ident + )); + return Ok(()); + } + + self.visit_value(&token::Value::TEXT(ident.ident.into())) + } + #[cfg(not(feature = "ast-span"))] + Some(Occur::Optiona) | None => { + if is_ident_string_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Text(_))) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + + return Ok(()); + } + + if is_ident_integer_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Integer(_))) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } + + if is_ident_bool_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Bool(_))) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } + + if is_ident_null_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Null)) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } + + if is_ident_byte_string_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Bytes(_))) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } + + if is_ident_float_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Null)) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } + + if token::lookup_ident(ident.ident) + .in_standard_prelude() + .is_some() + { + self.add_error(format!( + "expected object value of type {}, got object", + ident.ident + )); + return Ok(()); + } + + self.visit_value(&token::Value::TEXT(ident.ident.into())) + } + Some(occur) => { + let mut errors = Vec::new(); + + if is_ident_string_data_type(self.cddl, ident) { + let values_to_validate = m + .iter() + .filter_map(|(k, v)| { + if let Some(keys) = &self.validated_keys { + if !keys.contains(k) { + if matches!(k, Value::Text(_)) { + Some(v.clone()) + } else { + errors.push(format!("key of type {} required, got {:?}", ident, k)); + None + } } else { - errors.push(format!("key of type {} required, got {:?}", ident, k)); None } + } else if matches!(k, Value::Text(_)) { + Some(v.clone()) } else { + errors.push(format!("key of type {} required, got {:?}", ident, k)); None } - } else if matches!(k, Value::Text(_)) { - Some(v.clone()) - } else { - errors.push(format!("key of type {} required, got {:?}", ident, k)); - None - } - }) - .collect::>(); - - self.values_to_validate = Some(values_to_validate); - } - - if is_ident_integer_data_type(self.cddl, ident) { - let mut errors = Vec::new(); - let values_to_validate = m - .iter() - .filter_map(|(k, v)| { - if let Some(keys) = &self.validated_keys { - if !keys.contains(k) { - if matches!(k, Value::Integer(_)) { - Some(v.clone()) + }) + .collect::>(); + + self.values_to_validate = Some(values_to_validate); + } + + if is_ident_integer_data_type(self.cddl, ident) { + let mut errors = Vec::new(); + let values_to_validate = m + .iter() + .filter_map(|(k, v)| { + if let Some(keys) = &self.validated_keys { + if !keys.contains(k) { + if matches!(k, Value::Integer(_)) { + Some(v.clone()) + } else { + errors.push(format!("key of type {} required, got {:?}", ident, k)); + None + } } else { - errors.push(format!("key of type {} required, got {:?}", ident, k)); None } + } else if matches!(k, Value::Integer(_)) { + Some(v.clone()) } else { + errors.push(format!("key of type {} required, got {:?}", ident, k)); None } - } else if matches!(k, Value::Integer(_)) { - Some(v.clone()) - } else { - errors.push(format!("key of type {} required, got {:?}", ident, k)); - None - } - }) - .collect::>(); - - self.values_to_validate = Some(values_to_validate); - } - - if is_ident_bool_data_type(self.cddl, ident) { - let mut errors = Vec::new(); - let values_to_validate = m - .iter() - .filter_map(|(k, v)| { - if let Some(keys) = &self.validated_keys { - if !keys.contains(k) { - if matches!(k, Value::Bool(_)) { - Some(v.clone()) + }) + .collect::>(); + + self.values_to_validate = Some(values_to_validate); + } + + if is_ident_bool_data_type(self.cddl, ident) { + let mut errors = Vec::new(); + let values_to_validate = m + .iter() + .filter_map(|(k, v)| { + if let Some(keys) = &self.validated_keys { + if !keys.contains(k) { + if matches!(k, Value::Bool(_)) { + Some(v.clone()) + } else { + errors.push(format!("key of type {} required, got {:?}", ident, k)); + None + } } else { - errors.push(format!("key of type {} required, got {:?}", ident, k)); None } + } else if matches!(k, Value::Bool(_)) { + Some(v.clone()) } else { + errors.push(format!("key of type {} required, got {:?}", ident, k)); None } - } else if matches!(k, Value::Bool(_)) { - Some(v.clone()) - } else { - errors.push(format!("key of type {} required, got {:?}", ident, k)); - None - } - }) - .collect::>(); - - self.values_to_validate = Some(values_to_validate); - } - - if is_ident_byte_string_data_type(self.cddl, ident) { - let mut errors = Vec::new(); - let values_to_validate = m - .iter() - .filter_map(|(k, v)| { - if let Some(keys) = &self.validated_keys { - if !keys.contains(k) { - if matches!(k, Value::Bytes(_)) { - Some(v.clone()) + }) + .collect::>(); + + self.values_to_validate = Some(values_to_validate); + } + + if is_ident_byte_string_data_type(self.cddl, ident) { + let mut errors = Vec::new(); + let values_to_validate = m + .iter() + .filter_map(|(k, v)| { + if let Some(keys) = &self.validated_keys { + if !keys.contains(k) { + if matches!(k, Value::Bytes(_)) { + Some(v.clone()) + } else { + errors.push(format!("key of type {} required, got {:?}", ident, k)); + None + } } else { - errors.push(format!("key of type {} required, got {:?}", ident, k)); None } + } else if matches!(k, Value::Bytes(_)) { + Some(v.clone()) } else { + errors.push(format!("key of type {} required, got {:?}", ident, k)); None } - } else if matches!(k, Value::Bytes(_)) { - Some(v.clone()) - } else { - errors.push(format!("key of type {} required, got {:?}", ident, k)); - None - } - }) - .collect::>(); - - self.values_to_validate = Some(values_to_validate); - } - - if is_ident_null_data_type(self.cddl, ident) { - let mut errors = Vec::new(); - let values_to_validate = m - .iter() - .filter_map(|(k, v)| { - if let Some(keys) = &self.validated_keys { - if !keys.contains(k) { - if matches!(k, Value::Null) { - Some(v.clone()) + }) + .collect::>(); + + self.values_to_validate = Some(values_to_validate); + } + + if is_ident_null_data_type(self.cddl, ident) { + let mut errors = Vec::new(); + let values_to_validate = m + .iter() + .filter_map(|(k, v)| { + if let Some(keys) = &self.validated_keys { + if !keys.contains(k) { + if matches!(k, Value::Null) { + Some(v.clone()) + } else { + errors.push(format!("key of type {} required, got {:?}", ident, k)); + None + } } else { - errors.push(format!("key of type {} required, got {:?}", ident, k)); None } + } else if matches!(k, Value::Null) { + Some(v.clone()) } else { + errors.push(format!("key of type {} required, got {:?}", ident, k)); None } - } else if matches!(k, Value::Null) { - Some(v.clone()) - } else { - errors.push(format!("key of type {} required, got {:?}", ident, k)); - None - } - }) - .collect::>(); - - self.values_to_validate = Some(values_to_validate); - } - - if is_ident_float_data_type(self.cddl, ident) { - let mut errors = Vec::new(); - let values_to_validate = m - .iter() - .filter_map(|(k, v)| { - if let Some(keys) = &self.validated_keys { - if !keys.contains(k) { - if matches!(k, Value::Float(_)) { - Some(v.clone()) + }) + .collect::>(); + + self.values_to_validate = Some(values_to_validate); + } + + if is_ident_float_data_type(self.cddl, ident) { + let mut errors = Vec::new(); + let values_to_validate = m + .iter() + .filter_map(|(k, v)| { + if let Some(keys) = &self.validated_keys { + if !keys.contains(k) { + if matches!(k, Value::Float(_)) { + Some(v.clone()) + } else { + errors.push(format!("key of type {} required, got {:?}", ident, k)); + None + } } else { - errors.push(format!("key of type {} required, got {:?}", ident, k)); None } + } else if matches!(k, Value::Float(_)) { + Some(v.clone()) } else { + errors.push(format!("key of type {} required, got {:?}", ident, k)); None } - } else if matches!(k, Value::Float(_)) { - Some(v.clone()) - } else { - errors.push(format!("key of type {} required, got {:?}", ident, k)); - None - } - }) - .collect::>(); - - self.values_to_validate = Some(values_to_validate); - } + }) + .collect::>(); - // If key validation error occurs, return early before checking occurrences - if !errors.is_empty() { - for e in errors.into_iter() { - self.add_error(e); + self.values_to_validate = Some(values_to_validate); } - return Ok(()); - } - - #[cfg(feature = "ast-span")] - if let Occur::ZeroOrMore(_) | Occur::OneOrMore(_) = occur { - if let Occur::OneOrMore(_) = occur { - if m.is_empty() { - self.add_error(format!( - "map cannot be empty, one or more entries with key type {} required", - ident - )); - return Ok(()); + // If key validation error occurs, return early before checking occurrences + if !errors.is_empty() { + for e in errors.into_iter() { + self.add_error(e); } + + return Ok(()); } - } else if let Occur::Exact { lower, upper, .. } = occur { - if let Some(values_to_validate) = &self.values_to_validate { - if let Some(lower) = lower { - if let Some(upper) = upper { - if values_to_validate.len() < *lower || values_to_validate.len() > *upper { - if lower == upper { - self.add_error(format!( - "object must contain exactly {} entries of key of type {}", - lower, ident, - )); - } else { - self.add_error(format!( - "object must contain between {} and {} entries of key of type {}", - lower, upper, ident, - )); + + #[cfg(feature = "ast-span")] + if let Occur::ZeroOrMore(_) | Occur::OneOrMore(_) = occur { + if let Occur::OneOrMore(_) = occur { + if m.is_empty() { + self.add_error(format!( + "map cannot be empty, one or more entries with key type {} required", + ident + )); + return Ok(()); + } + } + } else if let Occur::Exact { lower, upper, .. } = occur { + if let Some(values_to_validate) = &self.values_to_validate { + if let Some(lower) = lower { + if let Some(upper) = upper { + if values_to_validate.len() < *lower || values_to_validate.len() > *upper { + if lower == upper { + self.add_error(format!( + "object must contain exactly {} entries of key of type {}", + lower, ident, + )); + } else { + self.add_error(format!( + "object must contain between {} and {} entries of key of type {}", + lower, upper, ident, + )); + } + + return Ok(()); } + } + + if values_to_validate.len() < *lower { + self.add_error(format!( + "object must contain at least {} entries of key of type {}", + lower, ident, + )); return Ok(()); } } - if values_to_validate.len() < *lower { - self.add_error(format!( - "object must contain at least {} entries of key of type {}", - lower, ident, - )); + if let Some(upper) = upper { + if values_to_validate.len() > *upper { + self.add_error(format!( + "object must contain no more than {} entries of key of type {}", + upper, ident, + )); - return Ok(()); + return Ok(()); + } } + + return Ok(()); } + } - if let Some(upper) = upper { - if values_to_validate.len() > *upper { + #[cfg(not(feature = "ast-span"))] + if let Occur::ZeroOrMore | Occur::OneOrMore = occur { + if let Occur::OneOrMore = occur { + if o.is_empty() { self.add_error(format!( - "object must contain no more than {} entries of key of type {}", - upper, ident, + "object cannot be empty, one or more entries with key type {} required", + ident )); - return Ok(()); } } + } else if let Occur::Exact { lower, upper } = occur { + if let Some(values_to_validate) = &self.values_to_validate { + if let Some(lower) = lower { + if let Some(upper) = upper { + if values_to_validate.len() < *lower || values_to_validate.len() > *upper { + if lower == upper { + self.add_error(format!( + "object must contain exactly {} entries of key of type {}", + lower, ident, + )); + } else { + self.add_error(format!( + "object must contain between {} and {} entries of key of type {}", + lower, upper, ident, + )); + } - return Ok(()); - } - } - - #[cfg(not(feature = "ast-span"))] - if let Occur::ZeroOrMore | Occur::OneOrMore = occur { - if let Occur::OneOrMore = occur { - if o.is_empty() { - self.add_error(format!( - "object cannot be empty, one or more entries with key type {} required", - ident - )); - return Ok(()); - } - } - } else if let Occur::Exact { lower, upper } = occur { - if let Some(values_to_validate) = &self.values_to_validate { - if let Some(lower) = lower { - if let Some(upper) = upper { - if values_to_validate.len() < *lower || values_to_validate.len() > *upper { - if lower == upper { - self.add_error(format!( - "object must contain exactly {} entries of key of type {}", - lower, ident, - )); - } else { - self.add_error(format!( - "object must contain between {} and {} entries of key of type {}", - lower, upper, ident, - )); + return Ok(()); } + } + + if values_to_validate.len() < *lower { + self.add_error(format!( + "object must contain at least {} entries of key of type {}", + lower, ident, + )); return Ok(()); } } - if values_to_validate.len() < *lower { - self.add_error(format!( - "object must contain at least {} entries of key of type {}", - lower, ident, - )); + if let Some(upper) = upper { + if values_to_validate.len() > *upper { + self.add_error(format!( + "object must contain no more than {} entries of key of type {}", + upper, ident, + )); - return Ok(()); + return Ok(()); + } } - } - if let Some(upper) = upper { - if values_to_validate.len() > *upper { - self.add_error(format!( - "object must contain no more than {} entries of key of type {}", - upper, ident, - )); + return Ok(()); + } + } - return Ok(()); - } + if is_ident_string_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Text(_))) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); } return Ok(()); } - } - } - if is_ident_string_data_type(self.cddl, ident) && !self.validating_value { - if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Text(_))) { - self - .validated_keys - .get_or_insert(vec![k.clone()]) - .push(k.clone()); - self.object_value = Some(v.clone()); - let _ = write!(self.cbor_location, "/{:?}", v); - } else { - self.add_error(format!("map requires entry key of type {}", ident)); - } + if is_ident_integer_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Integer(_))) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } - return Ok(()); - } + if is_ident_bool_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Bool(_))) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } - if is_ident_integer_data_type(self.cddl, ident) && !self.validating_value { - if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Integer(_))) { - self - .validated_keys - .get_or_insert(vec![k.clone()]) - .push(k.clone()); - self.object_value = Some(v.clone()); - let _ = write!(self.cbor_location, "/{:?}", v); - } else { - self.add_error(format!("map requires entry key of type {}", ident)); - } - return Ok(()); - } + if is_ident_null_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Null)) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } - if is_ident_bool_data_type(self.cddl, ident) && !self.validating_value { - if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Bool(_))) { - self - .validated_keys - .get_or_insert(vec![k.clone()]) - .push(k.clone()); - self.object_value = Some(v.clone()); - let _ = write!(self.cbor_location, "/{:?}", v); - } else { - self.add_error(format!("map requires entry key of type {}", ident)); - } - return Ok(()); - } + if is_ident_byte_string_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Bytes(_))) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } - if is_ident_null_data_type(self.cddl, ident) && !self.validating_value { - if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Null)) { - self - .validated_keys - .get_or_insert(vec![k.clone()]) - .push(k.clone()); - self.object_value = Some(v.clone()); - let _ = write!(self.cbor_location, "/{:?}", v); - } else { - self.add_error(format!("map requires entry key of type {}", ident)); - } - return Ok(()); - } + if is_ident_float_data_type(self.cddl, ident) && !self.validating_value { + if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Null)) { + self + .validated_keys + .get_or_insert(vec![k.clone()]) + .push(k.clone()); + self.object_value = Some(v.clone()); + let _ = write!(self.cbor_location, "/{:?}", v); + } else { + self.add_error(format!("map requires entry key of type {}", ident)); + } + return Ok(()); + } - if is_ident_byte_string_data_type(self.cddl, ident) && !self.validating_value { - if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Bytes(_))) { - self - .validated_keys - .get_or_insert(vec![k.clone()]) - .push(k.clone()); - self.object_value = Some(v.clone()); - let _ = write!(self.cbor_location, "/{:?}", v); - } else { - self.add_error(format!("map requires entry key of type {}", ident)); - } - return Ok(()); - } + if token::lookup_ident(ident.ident) + .in_standard_prelude() + .is_some() + { + self.add_error(format!( + "expected object value of type {}, got object", + ident.ident + )); + return Ok(()); + } - if is_ident_float_data_type(self.cddl, ident) && !self.validating_value { - if let Some((k, v)) = m.iter().find(|(k, _)| matches!(k, Value::Null)) { - self - .validated_keys - .get_or_insert(vec![k.clone()]) - .push(k.clone()); - self.object_value = Some(v.clone()); - let _ = write!(self.cbor_location, "/{:?}", v); - } else { - self.add_error(format!("map requires entry key of type {}", ident)); + self.visit_value(&token::Value::TEXT(ident.ident.into())) } - return Ok(()); - } - - if token::lookup_ident(ident.ident) - .in_standard_prelude() - .is_some() - { - self.add_error(format!( - "expected object value of type {}, got object", - ident.ident - )); - return Ok(()); } - - self.visit_value(&token::Value::TEXT(ident.ident.into())) } _ => { if let Some(cut_value) = self.cut_value.take() { From b199c282b6017214be922fdd6a1926ad43e8cd16 Mon Sep 17 00:00:00 2001 From: anweiss <2326106+anweiss@users.noreply.github.com> Date: Mon, 3 Oct 2022 12:12:50 -0400 Subject: [PATCH 3/3] fix style lints --- src/ast.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 14f51b41..38e0a638 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -513,7 +513,7 @@ pub struct GenericParams<'a> { /// Generic parameter #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, PartialEq, Clone)] +#[derive(Debug, PartialEq, Eq, Clone)] pub struct GenericParam<'a> { /// Generic parameter pub param: Identifier<'a>, @@ -2280,7 +2280,7 @@ impl<'a> GroupEntry<'a> { /// Optional comma #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, Clone, PartialEq, Default)] +#[derive(Debug, Clone, PartialEq, Eq, Default)] pub struct OptionalComma<'a> { /// Optional comma pub optional_comma: bool, @@ -2474,7 +2474,7 @@ impl<'a> fmt::Display for GroupEntry<'a> { /// Occurrence indicator #[cfg_attr(target_arch = "wasm32", derive(Serialize))] -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone, PartialEq, Eq)] pub struct Occurrence<'a> { /// Occurrence indicator pub occur: Occur,