Skip to content

Commit

Permalink
cleanup and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
anweiss committed Oct 14, 2022
1 parent b991d3e commit b283e70
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 27 deletions.
28 changes: 26 additions & 2 deletions src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,30 @@ pub enum CDDLType<'a, 'b: 'a> {
NonMemberKey(&'b NonMemberKey<'a>),
}

impl<'a, 'b: 'a> From<&'b CDDL<'a>> for CDDLType<'a, 'b> {
fn from(cddl: &'b CDDL<'a>) -> Self {
CDDLType::CDDL(cddl)
}
}

impl<'a, 'b: 'a> From<&'b Rule<'a>> for CDDLType<'a, 'b> {
fn from(rule: &'b Rule<'a>) -> Self {
CDDLType::Rule(rule)
}
}

impl<'a, 'b: 'a> From<&'b TypeRule<'a>> for CDDLType<'a, 'b> {
fn from(rule: &'b TypeRule<'a>) -> Self {
CDDLType::TypeRule(rule)
}
}

impl<'a, 'b: 'a> From<&'b Identifier<'a>> for CDDLType<'a, 'b> {
fn from(ident: &'b Identifier<'a>) -> Self {
CDDLType::Identifier(ident)
}
}

#[cfg(feature = "ast-comments")]
#[derive(Default, Debug, PartialEq, Eq, Clone)]
#[doc(hidden)]
Expand Down Expand Up @@ -526,7 +550,7 @@ impl<'a> fmt::Display for GroupRule<'a> {
/// ```abnf
/// genericparm = "<" S id S *("," S id S ) ">"
/// ```
#[derive(Debug, PartialEq, Clone, Default)]
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct GenericParams<'a> {
/// List of generic parameters
pub params: Vec<GenericParam<'a>>,
Expand Down Expand Up @@ -958,7 +982,7 @@ impl<'a> fmt::Display for Type1<'a> {
/// rangeop = "..." / ".."
/// ctlop = "." id
/// ```
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum RangeCtlOp<'a> {
/// Range operator
RangeOp {
Expand Down
2 changes: 1 addition & 1 deletion src/validator/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3442,7 +3442,7 @@ where
}

fn visit_occurrence(&mut self, o: &Occurrence<'a>) -> visitor::Result<Error<T>> {
self.occurrence = Some(o.occur.clone());
self.occurrence = Some(o.occur);

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/validator/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2496,7 +2496,7 @@ impl<'a, 'b> Visitor<'a, 'b, Error> for JSONValidator<'a> {
}

fn visit_occurrence(&mut self, o: &Occurrence<'a>) -> visitor::Result<Error> {
self.occurrence = Some(o.occur.clone());
self.occurrence = Some(o.occur);

Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions src/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ pub fn entry_counts_from_group<'a>(cddl: &'a CDDL, group: Group<'a>) -> Vec<Entr
GroupEntry::ValueMemberKey { ge, .. } => {
if idx == 1 {
if let Some(occur) = &ge.occur {
entry_occurrence = Some(occur.occur.clone())
entry_occurrence = Some(occur.occur)
}
}

Expand All @@ -964,7 +964,7 @@ pub fn entry_counts_from_group<'a>(cddl: &'a CDDL, group: Group<'a>) -> Vec<Entr
GroupEntry::InlineGroup { group, occur, .. } => {
if idx == 1 {
if let Some(occur) = occur {
entry_occurrence = Some(occur.occur.clone())
entry_occurrence = Some(occur.occur)
}
}

Expand All @@ -973,7 +973,7 @@ pub fn entry_counts_from_group<'a>(cddl: &'a CDDL, group: Group<'a>) -> Vec<Entr
GroupEntry::TypeGroupname { ge, .. } => {
if idx == 1 {
if let Some(occur) = &ge.occur {
entry_occurrence = Some(occur.occur.clone())
entry_occurrence = Some(occur.occur)
}
}

Expand Down
70 changes: 50 additions & 20 deletions src/validator/parent_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
use std::{borrow::Cow, fmt};

/// validation Result
pub type Result = std::result::Result<(), Error>;
pub type Result<T> = std::result::Result<T, Error>;

/// validation error
#[derive(Debug)]
Expand All @@ -28,9 +28,7 @@ impl fmt::Display for Error {

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
_ => None,
}
None
}
}

Expand Down Expand Up @@ -75,26 +73,28 @@ impl<'a, 'b: 'a> Node<'a, 'b> {
/// validator type
// #[derive(Clone)]
pub struct ParentVisitor<'a, 'b: 'a> {
cddl: &'a CDDL<'a>,
arena_tree: ArenaTree<'a, 'b>,
}

impl<'a, 'b: 'a> ParentVisitor<'a, 'b> {
pub fn new(cddl: &'a CDDL<'a>) -> Self {
ParentVisitor {
cddl,
pub fn new(cddl: &'a CDDL<'a>) -> Result<Self> {
let mut p = ParentVisitor {
arena_tree: ArenaTree {
arena: Vec::default(),
},
}
};

p.visit_cddl(cddl)?;

Ok(p)
}
}

impl<'a, 'b: 'a> ParentVisitor<'a, 'b> {
fn insert(&mut self, parent: usize, child: usize) -> Result {
fn insert(&mut self, parent: usize, child: usize) -> Result<()> {
match self.arena_tree.arena[child].parent {
Some(_) => {
return Err(Error::Overwrite);
// return Err(Error::Overwrite);
}
None => {
self.arena_tree.arena[child].parent = Some(parent);
Expand All @@ -107,6 +107,22 @@ impl<'a, 'b: 'a> ParentVisitor<'a, 'b> {
}
}

impl<'a, 'b: 'a> CDDLType<'a, 'b> {
pub fn parent(&self, visitor: &'b ParentVisitor<'a, 'b>) -> Option<&'b CDDLType<'a, 'b>> {
for node in visitor.arena_tree.arena.iter() {
if self == &node.val {
if let Some(parent_idx) = node.parent {
if let Some(parent) = visitor.arena_tree.arena.iter().nth(parent_idx) {
return Some(&parent.val);
}
}
}
}

None
}
}

impl<'a, 'b: 'a> Visitor<'a, 'b, Error> for ParentVisitor<'a, 'b> {
fn visit_cddl(&mut self, cddl: &'b CDDL<'a>) -> visitor::Result<Error> {
let parent = self.arena_tree.node(CDDLType::CDDL(cddl));
Expand Down Expand Up @@ -591,14 +607,28 @@ mod tests {
use super::*;

#[test]
fn testing() {
let c = cddl_from_str(
r#"a = b
b = "test""#,
true,
)
.unwrap();
let mut t = ParentVisitor::new(&c);
t.visit_cddl(&c).unwrap();
fn rule_parent_is_cddl() -> Result<()> {
let c = cddl_from_str(r#"a = "myrule""#, true).unwrap();
let t = ParentVisitor::new(&c).unwrap();
let rule = c.rules.first().unwrap();

assert_eq!(
CDDLType::from(rule).parent(&t).unwrap(),
&CDDLType::from(&c)
);

Ok(())
}

#[test]
fn type_rule_parent_is_rule() -> Result<()> {
let c = cddl_from_str(r#"a = "myrule""#, true).unwrap();
let t = ParentVisitor::new(&c).unwrap();

if let r @ Rule::Type { rule, .. } = c.rules.first().unwrap() {
assert_eq!(CDDLType::from(rule).parent(&t).unwrap(), &CDDLType::from(r));
}

Ok(())
}
}

0 comments on commit b283e70

Please sign in to comment.