Skip to content

Commit e342c95

Browse files
committed
wip
1 parent 01c3e1d commit e342c95

File tree

1 file changed

+11
-22
lines changed

1 file changed

+11
-22
lines changed

rust/cbork-cddl-parser/src/preprocessor/mod.rs renamed to rust/cbork-cddl-parser/src/preprocessor.rs

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,22 @@ use crate::parser::{cddl, rfc_8610, rfc_9165, Ast};
1414
pub(crate) fn process_ast(ast: Ast) -> anyhow::Result<Ast> {
1515
match ast {
1616
Ast::Rfc8610(ast) => {
17-
process_root(ast, rfc_8610::Rule::cddl, rfc_8610::Rule::expr).map(Ast::Rfc8610)
17+
process_root_and_filter(ast, rfc_8610::Rule::cddl, rfc_8610::Rule::expr)
18+
.map(Ast::Rfc8610)
1819
},
1920
Ast::Rfc9165(ast) => {
20-
process_root(ast, rfc_9165::Rule::cddl, rfc_9165::Rule::expr).map(Ast::Rfc9165)
21+
process_root_and_filter(ast, rfc_9165::Rule::cddl, rfc_9165::Rule::expr)
22+
.map(Ast::Rfc9165)
23+
},
24+
Ast::Cddl(ast) => {
25+
process_root_and_filter(ast, cddl::Rule::cddl, cddl::Rule::expr).map(Ast::Cddl)
2126
},
22-
Ast::Cddl(ast) => process_root(ast, cddl::Rule::cddl, cddl::Rule::expr).map(Ast::Cddl),
2327
}
2428
}
2529

26-
/// Process the root rule of the AST.
27-
/// Returns a vector of expressions of the underlying AST.
28-
fn process_root<R: RuleType>(
29-
ast: Vec<Pair<'_, R>>, root_rule: R, expr_rule: R,
30+
/// Process the root rule of the AST and filter out all non `expected_rule` rules.
31+
fn process_root_and_filter<R: RuleType>(
32+
ast: Vec<Pair<'_, R>>, root_rule: R, expected_rule: R,
3033
) -> anyhow::Result<Vec<Pair<'_, R>>> {
3134
let mut ast_iter = ast.into_iter();
3235
let ast_root = ast_iter.next().ok_or(anyhow!("Empty AST."))?;
@@ -36,20 +39,6 @@ fn process_root<R: RuleType>(
3639
);
3740
Ok(ast_root
3841
.into_inner()
39-
.filter(|pair| pair.as_rule() == expr_rule)
42+
.filter(|pair| pair.as_rule() == expected_rule)
4043
.collect())
4144
}
42-
43-
#[cfg(test)]
44-
mod tests {
45-
use super::*;
46-
use crate::parser::parse_cddl;
47-
48-
#[test]
49-
fn it_works() {
50-
let mut cddl = include_str!("../../tests/cddl/valid_rfc8610_simple_1.cddl").to_string();
51-
52-
let ast = parse_cddl(&mut cddl, &crate::Extension::CDDL).unwrap();
53-
process_ast(ast).unwrap();
54-
}
55-
}

0 commit comments

Comments
 (0)