clarkmcc / cel-rust

Common Expression Language interpreter written in Rust
https://crates.io/crates/cel-interpreter
MIT License
362 stars 18 forks source link

Regex support? #48

Closed alexsnaps closed 2 months ago

alexsnaps commented 3 months ago

This is more of a question really than an actual issue... for now at least.

If I read the cel spec properly, it is expected it'd support regular expressions, re2 based flavor.

Trying this out:

#[test]
fn test_matches() {
  let tests = vec![
      ("map", "{1: 'abc', 2: 'def', 3: 'ghi'}.all(key, key.matches('^[a-zA-Z]*$')) == true"),
      ("string", "'foobar'.matches('^[a-zA-Z]*$') == true"),
  ];

  for (name, script) in tests {
      assert_eq!(test_script(script, None), Ok(true.into()), "{}", name);
  }
}

I'm getting a Err(NoSuchKey("matches")) for both map & string tests. Couldn't see anything mentioning regular expressions neither. So here's the questions, is this a conscious decision to not support them? Looking around, I couldn't find a good candidate regex lib to start implementing that support actually... especially as in our use-case we're looking at targeting wasm...

Have you considered that side of the spec? Any conclusion you came to already?

clarkmcc commented 3 months ago

Definitely not intentional, it's been more a lack of demand than anything. I'll definitely get this on the priority list, or I would be happy to work through a PR with you if you'd like to take a stab at it as well. Otherwise I'll take a look later this week.

alexsnaps commented 3 months ago

Great! I'm certainly willing to have a shot at this, as if I can make it work here, I don't have to support two different code path in my own on-going work (see here, tho I'm rewriting the entire support for typing leveraging CEL, as that's how many types are actually represented with Envoy anyways). I'll look at supporting this while keeping it all working in wasm too then tho. I'll start looking around for a way to support that and will report my findings here. If you have any leads, now or later, please drop them here too 🙏 Thanks for the awesome work you've done on CEL for Rust already!

alexsnaps commented 3 months ago

Looks like regex would do the job... I'll start looking into leveraging it to add support for regex in the interpreter