feat(regex): add host-backed groups, alternation and captures

This commit is contained in:
Marqdo Agent committed 2026-11-21 10:58:06 +08:00
1 parent 94a13880c4
commit 28b178fc70
16 files changed
+289 -8

No files matched your search

+6
View File
@@ -312,6 +312,12 @@ fn regex_test_matches_anchors_classes_and_search() {
assert_eq!(items[9]["value"], false);
assert_eq!(items[10]["value"], "regex.invalid");
assert_eq!(items[11]["value"], "regex.unsupported");
assert_eq!(items[12]["value"], true);
assert_eq!(items[13]["value"], true);
let groups = items[14]["items"].as_array().expect("groups");
assert_eq!(groups[0]["value"], "aab");
assert_eq!(groups[1]["value"], "aa");
assert_eq!(groups[2]["value"], "b");
}
#[test]
+1
View File
@@ -15,6 +15,7 @@ rustls-native-certs = { workspace = true }
marqdo-plugin-abi = { workspace = true }
flate2 = { workspace = true }
getrandom = { workspace = true }
regex = { workspace = true }
glob = { workspace = true }
marqdo-span = { workspace = true }
marqdo-value = { workspace = true }
+76
View File
@@ -845,6 +845,82 @@ impl Host for StdHost {
self.record_resource_effect("resource.request", ctx, started, "process.run");
return Ok(result);
}
if operation == "regex.match" || operation == "regex.captures" {
if operation == "regex.match" {
if args.len() != 2 {
return Err(RuntimeError::new(
ErrorCategory::Arity,
ctx.span,
format!(
"resource operation `regex.match` expects 2 arguments, got {}",
args.len()
),
));
}
} else if args.len() != 2 {
return Err(RuntimeError::new(
ErrorCategory::Arity,
ctx.span,
format!(
"resource operation `regex.captures` expects 2 arguments, got {}",
args.len()
),
));
}
let pattern_type = args[0].type_name();
let Value::Text(pattern) = args[0].clone() else {
return Err(RuntimeError::type_error(
ctx.span,
format!("{operation} pattern must be Text, got {pattern_type}"),
));
};
let text_type = args[1].type_name();
let Value::Text(text) = args[1].clone() else {
return Err(RuntimeError::type_error(
ctx.span,
format!("{operation} text must be Text, got {text_type}"),
));
};
let regex = match regex::Regex::new(&pattern) {
Ok(regex) => regex,
Err(error) => {
return Ok(result_error(
"regex.invalid",
&format!("invalid regular expression: {error}"),
))
}
};
self.record_semantic(
"resource.request",
None,
Some(&ctx.module_id),
ctx.span,
Some(operation),
);
self.record_resource_effect("resource.request", ctx, started, operation);
let mut map = BTreeMap::new();
map.insert("ok".to_string(), Value::Bool(true));
if operation == "regex.match" {
map.insert("value".to_string(), Value::Bool(regex.is_match(&text)));
} else {
let mut payload = BTreeMap::new();
let captures = regex.captures(&text);
payload.insert("matched".to_string(), Value::Bool(captures.is_some()));
let groups = match captures {
Some(captures) => captures
.iter()
.map(|group| match group {
Some(group) => Value::text(group.as_str()),
None => Value::None,
})
.collect(),
None => Vec::new(),
};
payload.insert("groups".to_string(), Value::List(groups));
map.insert("value".to_string(), Value::Map(payload));
}
return Ok(Value::Map(map));
}
if operation == "compress.gzip" {
if args.len() != 1 {
return Err(RuntimeError::new(
@@ -1117,3 +1117,44 @@ fn compression_round_trips_and_rejects_invalid_gzip() {
assert_eq!(field(&invalid, "ok"), &Value::Bool(false));
assert_eq!(field(&invalid, "code"), &Value::text("compress.invalid"));
}
#[test]
fn regex_operations_support_groups_alternation_and_errors() {
let dir = tempfile::tempdir().unwrap();
let mut host = StdHost::new(SandboxMode::Project, Capabilities::project(), dir.path());
let ctx = context();
let matched = host
.resource_request(
"regex.match",
vec![Value::text("^(a+)(b|c)$"), Value::text("aab")],
&ctx,
)
.unwrap();
assert_eq!(field(&matched, "value"), &Value::Bool(true));
let captures = host
.resource_request(
"regex.captures",
vec![Value::text("^(a+)(b|c)$"), Value::text("aab")],
&ctx,
)
.unwrap();
let value = field(&captures, "value");
assert_eq!(field(value, "matched"), &Value::Bool(true));
assert_eq!(
field(value, "groups"),
&Value::List(vec![
Value::text("aab"),
Value::text("aa"),
Value::text("b")
])
);
let invalid = host
.resource_request(
"regex.match",
vec![Value::text("["), Value::text("a")],
&ctx,
)
.unwrap();
assert_eq!(field(&invalid, "ok"), &Value::Bool(false));
assert_eq!(field(&invalid, "code"), &Value::text("regex.invalid"));
}