etdl-core 0.2.0

ETDL runtime: BranchMonitor, retry policies, SLA anomaly detection, chaos injection, and telemetry for reliability-aware event-driven services
Documentation
//! Runtime helpers for generated ECEL conditions.
//!
//! ECEL's `in` and `matches` operators are not native Rust syntax, so generated
//! code lowers them to calls in this module. `in` becomes a linear-time
//! membership check; `matches` becomes a regular-expression match. The regex
//! engine is `regex` (a RE2-style linear-time engine), satisfying ETDL ยง6.5's
//! mandate that `matches` be a safe, linear-time RE2-compatible match.

use regex::Regex;

/// Returns true when `value` equals any element of `items`.
///
/// Used for ECEL `x in [a, b, c]` and `x in <array-typed path>`.
pub fn contains<T: PartialEq>(items: &[T], value: &T) -> bool {
    items.iter().any(|item| item == value)
}

/// Returns true when `value` matches the RE2-compatible pattern `pattern`.
///
/// Used for ECEL `x matches "pattern"`. The pattern is compiled per call;
/// a caller holding many evaluations should pre-compile with [`Regex`].
pub fn matches(value: &str, pattern: &str) -> bool {
    Regex::new(pattern)
        .map(|re| re.is_match(value))
        .unwrap_or(false)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn contains_finds_element() {
        assert!(contains(&[1, 2, 3], &2));
        assert!(!contains(&[1, 2, 3], &4));
        assert!(!contains::<i32>(&[], &1));
    }

    #[test]
    fn matches_re2() {
        assert!(matches("ORD-12345678", r"^ORD-[0-9]{8}$"));
        assert!(!matches("order-12345678", r"^ORD-[0-9]{8}$"));
    }

    #[test]
    fn invalid_pattern_is_false() {
        assert!(!matches("anything", "[unclosed"));
    }
}