cel-cxx 0.2.5

A high-performance, type-safe Rust interface for Common Expression Language (CEL), build on top of cel-cpp with zero-cost FFI bindings via cxx
Documentation
# Sets Extension

The sets extension provides set relationship functions for CEL. While there is no dedicated set type within CEL, this extension provides functionality for treating lists as sets and performing set operations.

## Table of Contents

- [Sets Extension]#sets-extension
  - [Table of Contents]#table-of-contents
  - [Overview]#overview
  - [Enabling the Extension]#enabling-the-extension
  - [Functions]#functions
    - [sets.contains()]#setscontains
    - [sets.equivalent()]#setsequivalent
    - [sets.intersects()]#setsintersects
  - [Usage Examples]#usage-examples
    - [Permission Checking]#permission-checking
    - [Data Validation]#data-validation
    - [Set Operations]#set-operations

## Overview

The sets extension enhances CEL with set relationship functions that operate on lists. Since CEL doesn't have a native set type, these functions treat lists as sets and provide basic functionality for determining set containment, equivalence, and intersection.

**Key Features:**
- Set containment checking
- Set equivalence testing
- Set intersection detection
- Works with any comparable types
- Uses standard CEL equality for comparisons

## Enabling the Extension

To enable the sets extension in your CEL environment:

```rust
let env = Env::builder()
    .with_ext_sets(true)
    .build()?;
```

## Functions

### sets.contains()

Returns whether the first list argument contains all elements in the second list argument.

**Syntax**: `sets.contains(list(T), list(T)) -> bool`

**Parameters**:
- `list1` (list): The container list
- `list2` (list): The elements to check for containment

**Returns**: `bool` - `true` if all elements in `list2` are present in `list1`

**Behavior**:
- Uses standard CEL equality to determine element presence
- If the second list is empty, always returns `true`
- Elements can be of any type
- Supports type coercion (e.g., `1`, `1.0`, `1u` are considered equal)

**Examples**:
```cel
sets.contains([], [])                    // true
sets.contains([], [1])                   // false
sets.contains([1, 2, 3, 4], [2, 3])      // true
sets.contains([1, 2.0, 3u], [1.0, 2u, 3]) // true
```

### sets.equivalent()

Returns whether the first and second list are set equivalent.

**Syntax**: `sets.equivalent(list(T), list(T)) -> bool`

**Parameters**:
- `list1` (list): First list to compare
- `list2` (list): Second list to compare

**Returns**: `bool` - `true` if the lists are set equivalent

**Behavior**:
- Lists are set equivalent if for every item in the first list, there is an equal element in the second
- Lists may not be the same size (duplicates don't affect equivalence)
- Uses standard CEL equality for comparisons
- Supports type coercion

**Examples**:
```cel
sets.equivalent([], [])                  // true
sets.equivalent([1], [1, 1])             // true
sets.equivalent([1], [1u, 1.0])          // true
sets.equivalent([1, 2, 3], [3u, 2.0, 1]) // true
```

### sets.intersects()

Returns whether the first list has at least one element whose value is equal to an element in the second list.

**Syntax**: `sets.intersects(list(T), list(T)) -> bool`

**Parameters**:
- `list1` (list): First list to compare
- `list2` (list): Second list to compare

**Returns**: `bool` - `true` if the lists have at least one common element

**Behavior**:
- If either list is empty, returns `false`
- Uses standard CEL equality for comparisons
- Supports type coercion
- Only needs one matching element to return `true`

**Examples**:
```cel
sets.intersects([1], [])                 // false
sets.intersects([1], [1, 2])             // true
sets.intersects([[1], [2, 3]], [[1, 2], [2, 3.0]]) // true
```

## Usage Examples

### Permission Checking

```cel
// Check if user has required permissions
cel.bind(user_permissions, ["read", "write", "execute"],
  cel.bind(required_permissions, ["read", "write"],
    sets.contains(user_permissions, required_permissions)
  )
)
// Result: true
```

### Data Validation

```cel
// Validate that submitted categories are from allowed list
cel.bind(allowed_categories, ["tech", "health", "finance", "education"],
  cel.bind(submitted_categories, ["tech", "health"],
    sets.contains(allowed_categories, submitted_categories)
  )
)
// Result: true
```

### Set Operations

```cel
// Check if two user groups have equivalent permissions
cel.bind(group_a_permissions, ["read", "write", "delete"],
  cel.bind(group_b_permissions, ["delete", "read", "write"],
    sets.equivalent(group_a_permissions, group_b_permissions)
  )
)
// Result: true

// Check if user has any admin permissions
cel.bind(user_permissions, ["read", "write"],
  cel.bind(admin_permissions, ["delete", "admin", "sudo"],
    sets.intersects(user_permissions, admin_permissions)
  )
)
// Result: false
```

The sets extension provides essential set operations for CEL expressions, enabling powerful data validation and permission checking scenarios while maintaining CEL's type safety and immutability guarantees.