Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DID test fixtures and regex escaping fixes #68

Merged
merged 11 commits into from
Nov 2, 2020
Merged
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = "0.8.2"
authors = ["Andrew Weiss <[email protected]>"]
readme = "README.md"
edition = "2018"
exclude = ["cddl-lsp/**/*", "www/**/*", ".github/**/*", ".devcontainer/**/*", "pkg/**/*", ".dockerignore", "Dockerfile"]
exclude = ["cddl-lsp/**/*", "www/**/*", ".github/**/*", ".devcontainer/**/*", "pkg/**/*", ".dockerignore", "Dockerfile", "tests/**/*"]

# Temporary workaround for https://github.com/rustwasm/wasm-pack/issues/886
[package.metadata.wasm-pack.profile.release]
Expand All @@ -27,8 +27,9 @@ codespan-reporting = "0.9"
itertools = "0.9"
lexical-core = "0.7"
regex = { version = "1.4", default-features = false, features = ["std"] }
regex-syntax = { version = "0.6", optional = true }
serde = { version = "1.0", optional = true, features = ["derive"] }
serde_cbor = { version = "0.11", optional = true }
serde_cbor = { version = "0.11.1", optional = true, default-features = false, features = ["std", "tags"] }
serde_json = { version = "1.0", optional = true, default-features = false }
uriparse = { version = "0.6", optional = true }
base64-url = { version = "1.4", optional = true }
Expand All @@ -50,7 +51,7 @@ wasm-bindgen-test = "0.3"

[features]
default = ["std"]
std = ["serde_json", "serde_cbor", "serde", "chrono", "wasm-bindgen", "clap", "crossterm", "uriparse", "base64-url"]
std = ["serde_json", "serde_cbor", "serde", "chrono", "wasm-bindgen", "clap", "crossterm", "uriparse", "base64-url", "regex-syntax"]
lsp = ["std"]

[[bin]]
Expand Down
34 changes: 18 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,21 @@ The first non-group rule defined by a CDDL data structure definition determines

The following types and features of CDDL are supported by this crate for validating JSON:

| CDDL | JSON |
| ---------------------- | ---------------------------------------------------------------------------------------------------------- |
| structs | objects |
| arrays | arrays<sup>[1](#arrays)</sup> |
| `text / tstr` | string |
| `uri` | string (valid RFC3986 URI) |
| `tdate` | string (valid RFC3339 date/time) |
| `b64url` | string (base64url-encoded) |
| `time` | number (valid UNIX timestamp integer in seconds) |
| `number / int / float` | number<sup>[2](#number)</sup> |
| `bool / true / false` | boolean |
| `null / nil` | null |
| `any` | any valid JSON |
| byte strings | not yet implemented |
| unwrap (`~`) | any JSON that matches unwrapped type from map, array or supported tag (`uri`, `tdate`, `b64url` or `time`) |
| CDDL | JSON |
| ---------------------- | ----------------------------------------------------------- |
| structs | objects |
| arrays | arrays<sup>[1](#arrays)</sup> |
| `text / tstr` | string |
| `uri` | string (valid RFC3986 URI) |
| `tdate` | string (valid RFC3339 date/time) |
| `b64url` | string (base64url-encoded) |
| `time` | number (valid UNIX timestamp integer in seconds) |
| `number / int / float` | number<sup>[2](#number)</sup> |
| `bool / true / false` | boolean |
| `null / nil` | null |
| `any` | any valid JSON |
| byte strings | not yet implemented |
| unwrap (`~`) | any JSON that matches unwrapped type from map, array or tag |

CDDL groups, generics, sockets/plugs and group-to-choice enumerations can all be used when validating JSON.

Expand Down Expand Up @@ -220,7 +220,7 @@ Below is the table of supported control operators:
| `.ne` | <g-emoji class="g-emoji" alias="heavy_check_mark" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/2714.png">✔️</g-emoji> |
| `.default` | <g-emoji class="g-emoji" alias="heavy_check_mark" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/2714.png">✔️</g-emoji> |

<a name="arrays">1</a>: When groups with multiple group entries are used to validate arrays, those entries with occurrence indicators are ignored due to complexities involved with processing these ambiguities. For proper JSON validation, avoid writing CDDL that looks like the following: `[ * a: int, b: tstr, ? c: int ]`.
<a name="arrays">1</a>: When groups with multiple group entries are used to validate arrays, occurrence indicators are "greedy" in that only the first occurrence indicator that is come across is used in the validation. Subsequent entries with occurrence indicators are ignored due to complexities involved with processing these ambiguities. For proper JSON validation, avoid writing CDDL that looks like the following: `[ * a: int, b: tstr, ? c: int ]`.

<a name="number">2</a>: While JSON itself does not distinguish between integers and floating-point numbers, this crate does provide the ability to validate numbers against a more specific numerical CBOR type, provided that its equivalent representation is allowed by JSON. Refer to [Appendix E.](https://tools.ietf.org/html/rfc8610#appendix-E) of the standard for more details on the implications of using CDDL with JSON numbers.

Expand Down Expand Up @@ -265,6 +265,8 @@ The following tags are supported when validating CBOR:
| `mime-message = #6.36(tstr)` | <g-emoji class="g-emoji" alias="heavy_check_mark" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/2714.png">✔️</g-emoji> |
| `cbor-any = #6.55799(any)` | <g-emoji class="g-emoji" alias="heavy_check_mark" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/2714.png">✔️</g-emoji> |

The `.bits`, `.cbor` and `.cborseq` control operators are not yet implemented.

## `no_std` support

Only the lexer and parser can be used in a `no_std` context provided that a heap allocator is available. This can be enabled by opting out of the default features in your `Cargo.toml` file as follows:
Expand Down
37 changes: 21 additions & 16 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,21 +231,21 @@
//! The following types and features of CDDL are supported by this crate for
//! validating JSON:
//!
//! | CDDL | JSON |
//! | ---------------------- | ---------------------------------------------------------------------------------------------------------- |
//! | structs | objects |
//! | arrays | arrays<sup>[1](#arrays)</sup> |
//! | `text / tstr` | string |
//! | `uri` | string (valid RFC3986 URI) |
//! | `tdate` | string (valid RFC3339 date/time) |
//! | `b64url` | string (base64url-encoded) |
//! | `time` | number (valid UNIX timestamp integer in seconds) |
//! | `number / int / float` | number<sup>[2](#number)</sup> |
//! | `bool / true / false` | boolean |
//! | `null / nil` | null |
//! | `any` | any valid JSON |
//! | byte strings | not yet implemented |
//! | unwrap (`~`) | any JSON that matches unwrapped type from map, array or supported tag (`uri`, `tdate`, `b64url` or `time`) |
//! | CDDL | JSON |
//! | ---------------------- | ----------------------------------------------------------- |
//! | structs | objects |
//! | arrays | arrays<sup>[1](#arrays)</sup> |
//! | `text / tstr` | string |
//! | `uri` | string (valid RFC3986 URI) |
//! | `tdate` | string (valid RFC3339 date/time) |
//! | `b64url` | string (base64url-encoded) |
//! | `time` | number (valid UNIX timestamp integer in seconds) |
//! | `number / int / float` | number<sup>[2](#number)</sup> |
//! | `bool / true / false` | boolean |
//! | `null / nil` | null |
//! | `any` | any valid JSON |
//! | byte strings | not yet implemented |
//! | unwrap (`~`) | any JSON that matches unwrapped type from map, array or tag |
//!
//! CDDL groups, generics, sockets/plugs and group-to-choice enumerations can
//! all be used when validating JSON.
Expand Down Expand Up @@ -282,7 +282,9 @@
//! | `.default` | <g-emoji class="g-emoji" alias="heavy_check_mark" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/2714.png">✔️</g-emoji> |
//!
//! <a name="arrays">1</a>: When groups with multiple group entries are used to
//! validate arrays, those entries with occurrence indicators are ignored due to
//! validate arrays, occurrence indicators are "greedy" in that only the first
//! occurrence indicator that is come across is used in the validation.
//! Subsequent entries with occurrence indicators are ignored due to
//! complexities involved with processing these ambiguities. For proper JSON
//! validation, avoid writing CDDL that looks like the following: `[ * a: int,
//! b: tstr, ? c: int ]`.
Expand Down Expand Up @@ -359,6 +361,9 @@
//! | `mime-message = #6.36(tstr)` | <g-emoji class="g-emoji" alias="heavy_check_mark" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/2714.png">✔️</g-emoji> |
//! | `cbor-any = #6.55799(any)` | <g-emoji class="g-emoji" alias="heavy_check_mark" fallback-src="https://github.githubassets.com/images/icons/emoji/unicode/2714.png">✔️</g-emoji> |
//!
//! The `.bits`, `.cbor` and `.cborseq` control operators are not yet
//! implemented.
//!
//! ## `no_std` support
//!
//! Only the lexer and parser can be used in a `no_std` context provided that a
Expand Down
13 changes: 11 additions & 2 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,14 @@ where
})
}
_ => {
// If type rule is an unwrap type, advance token after parsing type
let advance_token = matches!(self.cur_token, Token::UNWRAP);
let mut t = self.parse_type(None)?;

if advance_token {
self.next_token()?;
}

let comments_after_rule = if let Some(comments) = t.comments_after_type() {
Some(comments)
} else {
Expand Down Expand Up @@ -1419,15 +1425,18 @@ where
return Ok(grpchoice);
}

// Don't advance the token if it is a member key, comma or an opening or
// closing map/group delimiter. Otherwise, advance
// Don't advance the token if it is part of a member key, comma or an
// opening or closing map/group delimiter. Otherwise, advance
if !self.cur_token_is(Token::RPAREN)
&& !self.cur_token_is(Token::RBRACE)
&& !self.cur_token_is(Token::RBRACKET)
&& !self.cur_token_is(Token::LPAREN)
&& !self.cur_token_is(Token::LBRACE)
&& !self.cur_token_is(Token::LBRACKET)
&& !self.cur_token_is(Token::COMMA)
&& !self.cur_token_is(Token::OPTIONAL)
&& !self.cur_token_is(Token::ONEORMORE)
&& !self.cur_token_is(Token::ASTERISK)
&& !self.peek_token_is(&Token::COLON)
&& !self.peek_token_is(&Token::ARROWMAP)
&& !self.cur_token_is(Token::EOF)
Expand Down
Loading