Skip to main content

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 cached::proc_macro::cached;
5use tracing::instrument;
6
7pub mod ast;
8
9pub use ast::*;
10
11// Pre-generated LALRPOP parser - regenerate with:
12//   cargo build -p cala-cel-parser --features regenerate-parser
13//   cp target/debug/build/cala-cel-parser-*/out/cel.rs cala-cel-parser/src/cel.rs
14#[allow(clippy::all)]
15mod parser {
16    include!("cel.rs");
17}
18
19/// Error type for batch parsing
20#[derive(Debug)]
21pub struct ParseErrors(pub Vec<(usize, String)>);
22
23impl std::fmt::Display for ParseErrors {
24    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
25        write!(f, "{} parse error(s)", self.0.len())
26    }
27}
28
29impl std::error::Error for ParseErrors {}
30
31/// Instrumented wrapper for parsing CEL expressions with caching
32///
33/// This provides visibility into parsing operations while using the
34/// LALRPOP-generated parser internally. Parsing results are cached
35/// to avoid re-parsing the same expression multiple times.
36#[cached(size = 10000)]
37#[instrument(name = "cel.parse", skip(source), fields(expression = %source), err)]
38pub fn parse_expression(source: String) -> Result<Expression, String> {
39    parser::ExpressionParser::new()
40        .parse(&source)
41        .map_err(|e| e.to_string())
42}