cala_cel_parser/
lib.rs

1#![cfg_attr(feature = "fail-on-warnings", deny(warnings))]
2#![cfg_attr(feature = "fail-on-warnings", deny(clippy::all))]
3
4use lalrpop_util::lalrpop_mod;
5use tracing::instrument;
6
7pub mod ast;
8
9pub use ast::*;
10
11// Private module - use parse_expression() instead
12lalrpop_mod!(
13    #[allow(clippy::all)]
14    parser,
15    "/cel.rs"
16);
17
18/// Error type for batch parsing
19#[derive(Debug)]
20pub struct ParseErrors(pub Vec<(usize, String)>);
21
22impl std::fmt::Display for ParseErrors {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        write!(f, "{} parse error(s)", self.0.len())
25    }
26}
27
28impl std::error::Error for ParseErrors {}
29
30/// Instrumented wrapper for parsing CEL expressions
31///
32/// This provides visibility into parsing operations while using the
33/// LALRPOP-generated parser internally.
34#[instrument(name = "cel.parse", skip(source), fields(expression = %source), err)]
35pub fn parse_expression(source: &str) -> Result<Expression, String> {
36    parser::ExpressionParser::new()
37        .parse(source)
38        .map_err(|e| e.to_string())
39}