forked from microsoft/regorus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lock down ACI tests and more OPA test folders (microsoft#54)
Borrowed from rego-cpp Significantly (> 10 times) faster execution. $ cargo test -r --test aci aci/mount_device passed 9.597958ms aci/mount_overlay passed 10.159208ms aci/scratch_mount passed 8.598875ms aci/create_container passed 10.237292ms aci/shutdown_container passed 6.904084ms aci/scratch_unmount passed 6.530875ms aci/unmount_overlay passed 5.958875ms aci/unmount_device passed 5.657834ms aci/load_fragment passed 6.049917ms Signed-off-by: Anand Krishnamoorthi <[email protected]>
- Loading branch information
Showing
14 changed files
with
2,711 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
use crate::ast::{Expr, Ref}; | ||
use crate::builtins; | ||
use crate::builtins::utils::{ensure_args_count, ensure_string}; | ||
use crate::lexer::Span; | ||
use crate::value::Value; | ||
|
||
use std::collections::HashMap; | ||
|
||
use anyhow::{bail, Result}; | ||
use regex::Regex; | ||
|
||
pub fn register(m: &mut HashMap<&'static str, builtins::BuiltinFcn>) { | ||
m.insert("regex.is_valid", (is_valid, 1)); | ||
m.insert("regex.match", (regex_match, 2)); | ||
m.insert("regex.split", (regex_split, 2)); | ||
} | ||
|
||
fn is_valid(span: &Span, params: &[Ref<Expr>], args: &[Value]) -> Result<Value> { | ||
let name = "regex.is_valid"; | ||
ensure_args_count(span, name, params, args, 1)?; | ||
Ok(ensure_string(name, ¶ms[0], &args[0]) | ||
.map_or(Value::Bool(false), |p| Value::Bool(Regex::new(&p).is_ok()))) | ||
} | ||
|
||
pub fn regex_match(span: &Span, params: &[Ref<Expr>], args: &[Value]) -> Result<Value> { | ||
let name = "regex.match"; | ||
ensure_args_count(span, name, params, args, 2)?; | ||
let pattern = ensure_string(name, ¶ms[0], &args[0])?; | ||
let value = ensure_string(name, ¶ms[1], &args[1])?; | ||
|
||
let pattern = Regex::new(&pattern).or_else(|_| bail!(span.error("invalid regex")))?; | ||
Ok(Value::Bool(pattern.is_match(&value))) | ||
} | ||
|
||
pub fn regex_split(span: &Span, params: &[Ref<Expr>], args: &[Value]) -> Result<Value> { | ||
let name = "regex.split"; | ||
ensure_args_count(span, name, params, args, 2)?; | ||
let pattern = ensure_string(name, ¶ms[0], &args[0])?; | ||
let value = ensure_string(name, ¶ms[1], &args[1])?; | ||
|
||
let pattern = Regex::new(&pattern).or_else(|_| bail!(span.error("invalid regex")))?; | ||
Ok(Value::from_array( | ||
pattern | ||
.split(&value) | ||
.map(|s| Value::String(s.into())) | ||
.collect::<Vec<Value>>(), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.