From e342c95b224e185973d7e686b8a5cf98535de807 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Sat, 9 Nov 2024 17:59:50 +0200 Subject: [PATCH] wip --- .../{preprocessor/mod.rs => preprocessor.rs} | 33 +++++++------------ 1 file changed, 11 insertions(+), 22 deletions(-) rename rust/cbork-cddl-parser/src/{preprocessor/mod.rs => preprocessor.rs} (53%) diff --git a/rust/cbork-cddl-parser/src/preprocessor/mod.rs b/rust/cbork-cddl-parser/src/preprocessor.rs similarity index 53% rename from rust/cbork-cddl-parser/src/preprocessor/mod.rs rename to rust/cbork-cddl-parser/src/preprocessor.rs index 4458f29b..b20cc849 100644 --- a/rust/cbork-cddl-parser/src/preprocessor/mod.rs +++ b/rust/cbork-cddl-parser/src/preprocessor.rs @@ -14,19 +14,22 @@ use crate::parser::{cddl, rfc_8610, rfc_9165, Ast}; pub(crate) fn process_ast(ast: Ast) -> anyhow::Result { match ast { Ast::Rfc8610(ast) => { - process_root(ast, rfc_8610::Rule::cddl, rfc_8610::Rule::expr).map(Ast::Rfc8610) + process_root_and_filter(ast, rfc_8610::Rule::cddl, rfc_8610::Rule::expr) + .map(Ast::Rfc8610) }, Ast::Rfc9165(ast) => { - process_root(ast, rfc_9165::Rule::cddl, rfc_9165::Rule::expr).map(Ast::Rfc9165) + process_root_and_filter(ast, rfc_9165::Rule::cddl, rfc_9165::Rule::expr) + .map(Ast::Rfc9165) + }, + Ast::Cddl(ast) => { + process_root_and_filter(ast, cddl::Rule::cddl, cddl::Rule::expr).map(Ast::Cddl) }, - Ast::Cddl(ast) => process_root(ast, cddl::Rule::cddl, cddl::Rule::expr).map(Ast::Cddl), } } -/// Process the root rule of the AST. -/// Returns a vector of expressions of the underlying AST. -fn process_root( - ast: Vec>, root_rule: R, expr_rule: R, +/// Process the root rule of the AST and filter out all non `expected_rule` rules. +fn process_root_and_filter( + ast: Vec>, root_rule: R, expected_rule: R, ) -> anyhow::Result>> { let mut ast_iter = ast.into_iter(); let ast_root = ast_iter.next().ok_or(anyhow!("Empty AST."))?; @@ -36,20 +39,6 @@ fn process_root( ); Ok(ast_root .into_inner() - .filter(|pair| pair.as_rule() == expr_rule) + .filter(|pair| pair.as_rule() == expected_rule) .collect()) } - -#[cfg(test)] -mod tests { - use super::*; - use crate::parser::parse_cddl; - - #[test] - fn it_works() { - let mut cddl = include_str!("../../tests/cddl/valid_rfc8610_simple_1.cddl").to_string(); - - let ast = parse_cddl(&mut cddl, &crate::Extension::CDDL).unwrap(); - process_ast(ast).unwrap(); - } -}