Skip to content

Conversation

@hardbyte
Copy link
Owner

@hardbyte hardbyte commented Oct 13, 2025

Add substring() function to stdlib and improve logging

This PR introduces a new cel.stdlib module with Python implementations of CEL functions missing from upstream cel-rust, and improves logging levels for execution errors.

🎯 Changes

1. New Standard Library Module

  • Added python/cel/stdlib.py - Python implementations of missing CEL standard library functions

2. CLI Auto-loads stdlib

  • Updated python/cel/cli.py
    • CLI automatically includes all stdlib functions (no manual setup required)
    • Added substring to syntax highlighting
    • Added substring to autocomplete
    • Improved UX - stdlib functions work out of the box

3. Logging Improvements

  • Updated src/lib.rs (version 0.5.3)
    • Changed execution error logging from warn! to debug! level
    • Reduces noise in production environments
    • Errors still properly propagated to Python as exceptions

4. Documentation Updates

  • Updated docs/reference/cel-compliance.md

    • Added "Using cel.stdlib" section with examples
    • Clarified string indexing is not supported
    • Added function vs method syntax notes
  • Updated docs/reference/cli-reference.md

    • Added "Standard Library Functions" section
    • Documented CLI auto-includes stdlib
    • Provided usage examples
  • Updated error handling docs - Fixed error message assertions to use "Failed to parse expression"

🧪 Test Coverage

  • Added tests/test_stdlib.py (14 tests)

    • Basic functionality tests
    • Edge case coverage
    • Integration with CEL expressions
    • Context variable interactions
    • Upstream detection tests - Will alert when cel-rust adds native support
  • Updated tests/test_cli.py (2 new tests)

    • Verifies stdlib auto-loading in CLI
    • Tests stdlib functions with context variables
  • All tests passing: 342 passed, 1 skipped, 7 xfailed

📝 Usage Example

Programmatic API:

import cel
from cel.stdlib import add_stdlib_to_context

context = cel.Context()
add_stdlib_to_context(context)

result = cel.evaluate('substring("hello world", 0, 5)', context)  # → "hello"

CLI (auto-included):

$ cel 'substring("hello world", 0, 5)'
hello

$ cel 'substring("hello world", 6)'
world

🔍 Design Decisions

  1. Function syntax, not method syntax: substring("text", 0, 5) instead of "text".substring(0, 5)

    • Matches how Python functions are added to CEL context
    • Consistent with other custom functions pattern
  2. CLI auto-loads stdlib: Better UX - functions work without setup

  3. Upstream detection: Tests will fail when cel-rust adds native support, alerting us to switch to native implementation

  4. Extensible pattern: Easy to add more stdlib functions in the future

@hardbyte hardbyte changed the title Remove warning log Add substring() function to stdlib and improve logging Oct 14, 2025
@hardbyte hardbyte merged commit d18ff9e into main Oct 14, 2025
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants