Skip to content

Commit

Permalink
Merge pull request #85 from anweiss/map-fixes
Browse files Browse the repository at this point in the history
Map validation fixes
  • Loading branch information
anweiss authored Jan 25, 2021
2 parents 7b6e97d + f62deb4 commit 2a7236c
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 34 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>"]
readme = "README.md"
edition = "2018"
Expand All @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
15 changes: 8 additions & 7 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 { .. }
)
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
//!
Expand Down
7 changes: 4 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,9 +306,10 @@ where

/// Parses into a `CDDL` AST
pub fn parse_cddl(&mut self) -> Result<CDDL<'a>> {
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() {
Expand Down
11 changes: 11 additions & 0 deletions src/validator/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
_ => {
Expand Down
30 changes: 15 additions & 15 deletions src/validator/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();

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))
}
_ => {
Expand Down
12 changes: 7 additions & 5 deletions src/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
}
Expand Down

0 comments on commit 2a7236c

Please sign in to comment.