diff --git a/Cargo.toml b/Cargo.toml index 27a199ed..2f368172 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ repository = "https://github.com/anweiss/cddl" homepage = "https://cddl.anweiss.tech" categories = ["parser-implementations", "encoding", "development-tools", "wasm"] license = "MIT" -version = "0.8.5" +version = "0.8.6" authors = ["Andrew Weiss "] readme = "README.md" edition = "2018" @@ -23,7 +23,7 @@ base16 = { version = "0.2", default-features = false } base64 = { version = "0.13", default-features = false } chrono = { version = "0.4", optional = true } clap = { version = "2.33", optional = true } -codespan-reporting = "0.9" +codespan-reporting = "0.11" hexf-parse = "0.1" itertools = "0.9" lexical-core = "0.7" diff --git a/README.md b/README.md index bc277a7f..f2529092 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # cddl-rs -[![crates.io](https://img.shields.io/crates/v/cddl.svg)](https://crates.io/crates/cddl) [![docs.rs](https://docs.rs/cddl/badge.svg)](https://docs.rs/cddl) [![Publish packages](https://github.com/anweiss/cddl/workflows/Publish%20packages/badge.svg?branch=0.8.5&event=release)](https://github.com/anweiss/cddl/actions?query=workflow%3A%22Publish+packages%22) [![Build and Test](https://github.com/anweiss/cddl/workflows/Build%20and%20Test/badge.svg)](https://github.com/anweiss/cddl/actions?query=workflow%3A%22Build+and+Test%22) +[![crates.io](https://img.shields.io/crates/v/cddl.svg)](https://crates.io/crates/cddl) [![docs.rs](https://docs.rs/cddl/badge.svg)](https://docs.rs/cddl) [![Publish packages](https://github.com/anweiss/cddl/workflows/Publish%20packages/badge.svg?branch=0.8.6&event=release)](https://github.com/anweiss/cddl/actions?query=workflow%3A%22Publish+packages%22) [![Build and Test](https://github.com/anweiss/cddl/workflows/Build%20and%20Test/badge.svg)](https://github.com/anweiss/cddl/actions?query=workflow%3A%22Build+and+Test%22) > This crate was originally developed as a personal learning exercise for getting acquainted with Rust and parsing in general. There are likely more performant and stable libraries out there for parsing CDDL. While there are some examples of this crate being used in production, careful consideration should be made prior to using this crate as such. diff --git a/src/ast.rs b/src/ast.rs index cd99232f..268076e6 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -253,14 +253,15 @@ impl<'a> Rule<'a> { } = self { let type_check = |tc: &TypeChoice| { - matches!(tc.type1.type2, + matches!( + tc.type1.type2, Type2::Typename { .. } - | Type2::FloatValue { .. } - | Type2::IntValue { .. } - | Type2::UintValue { .. } - | Type2::TextValue { .. } - | Type2::B16ByteString { .. } - | Type2::B64ByteString { .. } + | Type2::FloatValue { .. } + | Type2::IntValue { .. } + | Type2::UintValue { .. } + | Type2::TextValue { .. } + | Type2::B16ByteString { .. } + | Type2::B64ByteString { .. } ) }; diff --git a/src/lib.rs b/src/lib.rs index c276eb6b..6444f290 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,7 +3,7 @@ //! [![crates.io](https://img.shields.io/crates/v/cddl.svg)](https://crates.io/crates/cddl) //! [![docs.rs](https://docs.rs/cddl/badge.svg)](https://docs.rs/cddl) //! [![Publish -//! packages](https://github.com/anweiss/cddl/workflows/Publish%20packages/badge.svg?branch=0.8.5&event=release)](https://github.com/anweiss/cddl/actions?query=workflow%3A%22Publish+packages%22) +//! packages](https://github.com/anweiss/cddl/workflows/Publish%20packages/badge.svg?branch=0.8.6&event=release)](https://github.com/anweiss/cddl/actions?query=workflow%3A%22Publish+packages%22) //! [![Build and //! Test](https://github.com/anweiss/cddl/workflows/Build%20and%20Test/badge.svg)](https://github.com/anweiss/cddl/actions?query=workflow%3A%22Build+and+Test%22) //! diff --git a/src/parser.rs b/src/parser.rs index db245d1d..c0c81964 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -306,9 +306,10 @@ where /// Parses into a `CDDL` AST pub fn parse_cddl(&mut self) -> Result> { - let mut c = CDDL::default(); - - c.comments = self.collect_comments()?; + let mut c = CDDL { + comments: self.collect_comments()?, + ..Default::default() + }; while self.cur_token != Token::EOF { match self.parse_rule() { diff --git a/src/validator/cbor.rs b/src/validator/cbor.rs index 12f64b69..63bfc8ec 100644 --- a/src/validator/cbor.rs +++ b/src/validator/cbor.rs @@ -2115,6 +2115,17 @@ impl<'a> Visitor<'a, ValidationError> for CBORValidator<'a> { return Ok(()); } + if token::lookup_ident(ident.ident) + .in_standard_prelude() + .is_some() + { + self.add_error(format!( + "expecting object value of type {}, got object", + ident.ident + )); + return Ok(()); + } + self.visit_value(&token::Value::TEXT(ident.ident)) } _ => { diff --git a/src/validator/json.rs b/src/validator/json.rs index 995db7dc..6e34676a 100644 --- a/src/validator/json.rs +++ b/src/validator/json.rs @@ -1466,33 +1466,33 @@ impl<'a> Visitor<'a, ValidationError> for JSONValidator<'a> { } if is_ident_string_data_type(self.cddl, ident) { - let mut errors = Vec::new(); let values_to_validate = o .iter() - .filter_map(|(k, v)| { - if let Some(keys) = &self.validated_keys { - if !keys.contains(&k) { - Some(v.clone()) - } else { - None - } - } else { - errors.push(format!("key of type {} required, got {:?}", ident, k)); - None - } + .filter_map(|(k, v)| match &self.validated_keys { + Some(keys) if !keys.contains(&k) => Some(v.clone()), + Some(_) => None, + None => Some(v.clone()), }) .collect::>(); self.values_to_validate = Some(values_to_validate); - for e in errors.into_iter() { - self.add_error(e); - } return Ok(()); } } } + if token::lookup_ident(ident.ident) + .in_standard_prelude() + .is_some() + { + self.add_error(format!( + "expecting object value of type {}, got object", + ident.ident + )); + return Ok(()); + } + self.visit_value(&token::Value::TEXT(ident.ident)) } _ => { diff --git a/src/validator/mod.rs b/src/validator/mod.rs index 8cf7aa32..14c9f820 100644 --- a/src/validator/mod.rs +++ b/src/validator/mod.rs @@ -60,10 +60,12 @@ pub fn unwrap_rule_from_ident<'a>(cddl: &'a CDDL, ident: &Identifier) -> Option< }, .. } if name == ident && !is_type_choice_alternate => { - if type_choices - .iter() - .any(|tc| matches!(tc.type1.type2, Type2::Map { .. } | Type2::Array { .. } | Type2::TaggedData { .. })) - { + if type_choices.iter().any(|tc| { + matches!( + tc.type1.type2, + Type2::Map { .. } | Type2::Array { .. } | Type2::TaggedData { .. } + ) + }) { Some(r) } else if let Some(ident) = type_choices.iter().find_map(|tc| { if let Type2::Typename { @@ -78,7 +80,7 @@ pub fn unwrap_rule_from_ident<'a>(cddl: &'a CDDL, ident: &Identifier) -> Option< } }) { unwrap_rule_from_ident(cddl, ident) - } else { + } else { None } }