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

Root type for validation #143

Merged
merged 1 commit into from
Oct 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/bin/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#[macro_use]
extern crate log;

use cddl::{cddl_from_str, validate_cbor_from_slice, validate_json_from_str};
use cddl::{
cddl_from_str, parser::root_type_name_from_cddl_str, validate_cbor_from_slice,
validate_json_from_str,
};
use clap::{ArgGroup, Args, Parser, Subcommand};

use simplelog::*;
Expand Down Expand Up @@ -146,6 +149,11 @@ fn main() -> Result<(), Box<dyn Error>> {

let cddl_str = fs::read_to_string(&validate.cddl)?;

info!(
"Root type for validation: {}",
root_type_name_from_cddl_str(&cddl_str)?
);

if let Some(files) = &validate.json {
for file in files {
let p = Path::new(file);
Expand All @@ -165,7 +173,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let r = validate_json_from_str(&cddl_str, &fs::read_to_string(file)?);

match r {
Ok(()) => {
Ok(_) => {
info!("Validation of {:?} is successful", p);
}
Err(e) => {
Expand Down Expand Up @@ -193,7 +201,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let c = validate_cbor_from_slice(&cddl_str, &data);

match c {
Ok(()) => {
Ok(_) => {
info!("Validation of {:?} is successful", p);
}
Err(e) => {
Expand All @@ -216,7 +224,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let r = validate_json_from_str(&cddl_str, json);

match r {
Ok(()) => {
Ok(_) => {
info!("Validation from stdin is successful");
}
Err(e) => {
Expand All @@ -230,7 +238,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let c = validate_cbor_from_slice(&cddl_str, &data);

match c {
Ok(()) => {
Ok(_) => {
info!("Validation from stdin is successful");
}
Err(e) => {
Expand Down
18 changes: 18 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3413,6 +3413,24 @@ pub fn cddl_from_str(input: &str, print_stderr: bool) -> std::result::Result<CDD
}
}

/// Identify root type name from CDDL input string
#[cfg(feature = "std")]
#[cfg(not(target_arch = "wasm32"))]
pub fn root_type_name_from_cddl_str(input: &str) -> std::result::Result<String, String> {
let cddl = cddl_from_str(input, false)?;

for r in cddl.rules.iter() {
// First type rule is root
if let Rule::Type { rule, .. } = r {
if rule.generic_params.is_none() {
return Ok(rule.name.to_string());
}
}
}

Err("cddl spec contains no root type".to_string())
}

impl<'a> CDDL<'a> {
/// Parses CDDL from a byte slice
#[cfg(not(target_arch = "wasm32"))]
Expand Down