mirror of
https://github.com/openai/codex.git
synced 2026-05-02 04:11:39 +03:00
refactor rules so no more cartesian product
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
use crate::decision::Decision;
|
||||
use crate::rule::Rule;
|
||||
use multimap::MultiMap;
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Policy {
|
||||
rules: Vec<Rule>,
|
||||
rules_by_program: MultiMap<String, Rule>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
@@ -15,18 +16,22 @@ pub struct Evaluation {
|
||||
}
|
||||
|
||||
impl Policy {
|
||||
pub fn new(rules: Vec<Rule>) -> Self {
|
||||
Self { rules }
|
||||
pub fn new(rules_by_program: MultiMap<String, Rule>) -> Self {
|
||||
Self { rules_by_program }
|
||||
}
|
||||
|
||||
pub fn rules(&self) -> &[Rule] {
|
||||
&self.rules
|
||||
pub fn rules(&self) -> &MultiMap<String, Rule> {
|
||||
&self.rules_by_program
|
||||
}
|
||||
|
||||
pub fn evaluate(&self, cmd: &[String]) -> Option<Evaluation> {
|
||||
let first = cmd.first()?;
|
||||
let Some(rules) = self.rules_by_program.get_vec(first) else {
|
||||
return None;
|
||||
};
|
||||
let mut matched_rules: Vec<crate::rule::RuleMatch> = Vec::new();
|
||||
let mut best_decision: Option<Decision> = None;
|
||||
for rule in &self.rules {
|
||||
for rule in rules {
|
||||
if let Some(matched) = rule.matches(cmd) {
|
||||
let decision = match best_decision {
|
||||
None => matched.decision,
|
||||
|
||||
Reference in New Issue
Block a user