rust_cel_parser/
lib.rs

1//! A parser for Google's Common Expression Language (CEL).
2//!
3//! This crate provides a parser that transforms CEL expression strings into a
4//! Abstract Syntax Tree (AST).
5//!
6//! # Key Features
7//!
8//! - **CEL Spec Compliance**: Parses a wide range of the CEL specification, including all literals, operators, and standard macros.
9//! - **Detailed AST**: A comprehensive and precise `ast::Expr` enum represents the structure of the parsed code.
10//! - **Ergonomic Traversal**: A built-in [visitor::Visitor] pattern allows you to inspect and analyze the AST without writing recursive boilerplate.
11//! - **Fluent AST Construction**: A [builder] module provides a clean, fluent API for programmatically creating AST nodes, perfect for testing or code generation.
12//! - **Rich Error Reporting**: On failure, returns a `CelParserError` with precise line and column information.
13//!
14//! # Getting Started: Parsing an Expression
15//!
16//! The main entry point to the library is the [parse_cel_program] function.
17//!
18//! ```
19//! use rust_cel_parser::{parse_cel_program, Expr, BinaryOperator, Literal};
20//!
21//! let expression = "request.auth.claims['group'] == 'admin'";
22//! let ast = parse_cel_program(expression).unwrap();
23//!
24//! // You can inspect the AST using helper methods
25//! if let Some((op, left, right)) = ast.as_binary_op() {
26//!     assert_eq!(op, BinaryOperator::Eq);
27//!     assert_eq!(right.as_literal(), Some(&Literal::String("admin".to_string())));
28//! } else {
29//!     panic!("Expected a binary operation");
30//! }
31//! ```
32//!
33//! # Building an AST Programmatically
34//!
35//! The [builder] module is ideal for creating expected ASTs in tests.
36//!
37//! ```
38//! use rust_cel_parser::{Expr, BinaryOperator, builder::*};
39//!
40//! // Represents the expression: `a.b + 1`
41//! let expected_ast = add(
42//!     field_access(ident("a"), "b"),
43//!     1 // .into() is called implicitly!
44//! );
45//!
46//! assert_eq!(
47//!     expected_ast,
48//!     Expr::BinaryOp {
49//!         op: BinaryOperator::Add,
50//!         left: Box::new(Expr::FieldAccess {
51//!             base: Box::new(Expr::Identifier("a".to_string())),
52//!             field: "b".to_string(),
53//!         }),
54//!         right: Box::new(Expr::from(1i64)),
55//!     }
56//! );
57//! ```
58
59mod ast;
60pub mod builder;
61mod error;
62mod parser;
63mod visitor;
64
65pub use ast::*;
66pub use error::*;
67pub use parser::*;
68pub use visitor::*;