mirror of
https://github.com/openai/codex.git
synced 2026-09-14 11:57:03 +00:00
58 lines
1.5 KiB
Rust
58 lines
1.5 KiB
Rust
use crate::decision::Decision;
|
|
use crate::rule::RuleMatch;
|
|
use crate::rule::RuleRef;
|
|
use multimap::MultiMap;
|
|
use serde::Deserialize;
|
|
use serde::Serialize;
|
|
|
|
#[derive(Clone, Debug)]
|
|
pub struct Policy {
|
|
rules_by_program: MultiMap<String, RuleRef>,
|
|
}
|
|
|
|
impl Policy {
|
|
pub fn new(rules_by_program: MultiMap<String, RuleRef>) -> Self {
|
|
Self { rules_by_program }
|
|
}
|
|
|
|
pub fn rules(&self) -> &MultiMap<String, RuleRef> {
|
|
&self.rules_by_program
|
|
}
|
|
|
|
pub fn check(&self, cmd: &[String]) -> Evaluation {
|
|
let rules = match cmd.first() {
|
|
Some(first) => match self.rules_by_program.get_vec(first) {
|
|
Some(rules) => rules,
|
|
None => return Evaluation::NoMatch,
|
|
},
|
|
None => return Evaluation::NoMatch,
|
|
};
|
|
|
|
let matched_rules: Vec<RuleMatch> =
|
|
rules.iter().filter_map(|rule| rule.matches(cmd)).collect();
|
|
match matched_rules.iter().map(RuleMatch::decision).max() {
|
|
Some(decision) => Evaluation::Match {
|
|
decision,
|
|
matched_rules,
|
|
},
|
|
None => Evaluation::NoMatch,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub enum Evaluation {
|
|
NoMatch,
|
|
Match {
|
|
decision: Decision,
|
|
matched_rules: Vec<RuleMatch>,
|
|
},
|
|
}
|
|
|
|
impl Evaluation {
|
|
pub fn is_match(&self) -> bool {
|
|
matches!(self, Self::Match { .. })
|
|
}
|
|
}
|