cel_std/lib.rs
1//! CEL standard library: `min`, `max`, `clamp`, and related numeric functions built on Rust's
2//! standard library, registered via [`cel_parser::OpLookup::register_operation`].
3//!
4//! # Examples
5//!
6//! ```rust
7//! use cel_parser::OpLookup;
8//!
9//! let mut lookup = OpLookup::new();
10//! cel_std::install(&mut lookup);
11//! ```
12
13mod math;
14
15/// Registers the CEL numeric standard library on `lookup`.
16///
17/// Includes `min`, `max`, `clamp`, `abs`, `signum`, `sqrt`, `floor`, `ceil`,
18/// `trunc`, `fract`, `round_ties_even`, `round`, `sin`, `cos`, `tan`, `asin`,
19/// `acos`, `atan`, `atan2`, `sin_cos`, `to_radians`, `to_degrees`, `hypot`,
20/// `cbrt`, `powi`, `powf`, `exp`, `exp2`, `ln`, `log`, `log2`, `log10`,
21/// `recip`, `mul_add`, `rem_euclid`, and `copysign`.
22///
23/// Both `sin(x)` and `x.sin()` use the same Rust floating-point semantics.
24/// Float operations accept `f32` and `f64` without implicit conversions.
25/// `powi` takes an `i32` exponent; `sin_cos` returns a CEL `(F, F)` tuple
26/// ordered as sine then cosine, where `F` is the input float type.
27///
28/// These functions use persistent named registrations, so they are always reachable,
29/// including inside closure bodies. Library functions are never isolated; only
30/// transient, per-declaration scopes (pushed via [`cel_parser::OpLookup::push_scope`])
31/// are isolated during nested body compilation.
32///
33/// - Precondition: No standard numeric signature is already installed.
34/// Independent libraries may add disjoint overloads before or after installation.
35/// - Complexity: O(s²) in the number of installed operation signatures.
36///
37/// # Examples
38/// ```
39/// let mut lookup = cel_parser::OpLookup::new();
40/// cel_std::install(&mut lookup);
41/// assert_eq!(lookup.operation_signatures("min").count(), 14);
42/// let mut parser = cel_parser::CELParser::new(lookup);
43/// let mut value = parser.parse_str("(90.0).to_radians().sin()").unwrap();
44/// assert!((value.call0::<f64>().unwrap() - 1.0).abs() < 1e-12);
45/// ```
46pub fn install(lookup: &mut cel_parser::OpLookup) {
47 math::install(lookup).expect("CEL standard-library signatures must be installed once");
48}