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 lalrpop_util::lalrpop_mod;
6use tracing::instrument;
7
8pub mod ast;
9
10pub use ast::*;
11
12// Private module - use parse_expression() instead
13lalrpop_mod!(
14    #[allow(clippy::all)]
15    parser,
16    "/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}