docstrings and other cleanup

This commit is contained in:
kevin zhao
2025-11-12 18:51:32 -05:00
parent 3162a77e2a
commit ceea9075ea
4 changed files with 13 additions and 8 deletions

View File

@@ -207,12 +207,11 @@ fn policy_builtins(builder: &mut GlobalsBuilder) {
let rules: Vec<Rule> = first_token
.alternatives()
.iter()
.zip(std::iter::repeat(rest.clone()))
.map(|(head, rest)| {
.map(|head| {
Rule::Prefix(PrefixRule {
pattern: PrefixPattern {
first: Arc::from(head.as_str()),
rest,
rest: rest.clone(),
},
decision,
})

View File

@@ -59,6 +59,7 @@ impl Evaluation {
}
}
/// Count how many rules match each provided example and error if any example is unmatched.
pub(crate) fn validate_match_examples(rules: &[Rule], matches: &[Vec<String>]) -> Result<()> {
let match_counts = rules.iter().fold(vec![0; matches.len()], |counts, rule| {
counts
@@ -71,10 +72,15 @@ pub(crate) fn validate_match_examples(rules: &[Rule], matches: &[Vec<String>]) -
let unmatched_examples: Vec<String> = matches
.iter()
.zip(&match_counts)
.filter(|(_, count)| **count == 0)
.map(|(example, _)| {
try_join(example.iter().map(String::as_str))
.unwrap_or_else(|_| "unable to render example".to_string())
.filter_map(|(example, count)| {
if *count == 0 {
Some(
try_join(example.iter().map(String::as_str))
.unwrap_or_else(|_| "unable to render example".to_string()),
)
} else {
None
}
})
.collect();

View File

@@ -139,6 +139,7 @@ impl Rule {
}
}
/// Return a boolean for each example indicating whether this rule matches it.
pub fn validate_matches(&self, matches: &[Vec<String>]) -> Vec<bool> {
match self {
Self::Prefix(rule) => rule.validate_matches(matches),

View File

@@ -1,7 +1,6 @@
use std::sync::Arc;
use codex_execpolicy2::Decision;
use codex_execpolicy2::Error;
use codex_execpolicy2::Evaluation;
use codex_execpolicy2::PolicyParser;
use codex_execpolicy2::Rule;