serializing noMatch as an object

This commit is contained in:
kevin zhao
2025-11-19 21:34:39 +00:00
parent 2b8cdc7be3
commit 876ade0445
5 changed files with 23 additions and 14 deletions
+3 -3
View File
@@ -109,7 +109,7 @@ fn evaluate_with_policy(
}
Decision::Allow => Some(ApprovalRequirement::Skip),
},
Evaluation::NoMatch => None,
Evaluation::NoMatch { .. } => None,
}
}
@@ -206,7 +206,7 @@ mod tests {
let commands = [vec!["rm".to_string()]];
assert!(matches!(
policy.check_multiple(commands.iter()),
Evaluation::NoMatch
Evaluation::NoMatch { .. }
));
assert!(!temp_dir.path().join(POLICY_DIR_NAME).exists());
}
@@ -259,7 +259,7 @@ mod tests {
let command = [vec!["ls".to_string()]];
assert!(matches!(
policy.check_multiple(command.iter()),
Evaluation::NoMatch
Evaluation::NoMatch { .. }
));
}
+2 -2
View File
@@ -34,7 +34,7 @@ cargo run -p codex-execpolicy2 -- check --policy path/to/policy.codexpolicy git
```
- Example outcomes:
- Match: `{"match": { ... "decision": "allow" ... }}`
- No match: `"noMatch"`
- No match: `{"noMatch": {}}`
## Response shapes
- Match:
@@ -56,7 +56,7 @@ cargo run -p codex-execpolicy2 -- check --policy path/to/policy.codexpolicy git
- No match:
```json
"noMatch"
{"noMatch": {}}
```
- `matchedRules` lists every rule whose prefix matched the command; `matchedPrefix` is the exact prefix that matched.
+2 -2
View File
@@ -1,14 +1,14 @@
pub mod execpolicycheck;
pub mod decision;
pub mod error;
pub mod execpolicycheck;
pub mod parser;
pub mod policy;
pub mod rule;
pub use execpolicycheck::ExecPolicyCheckCommand;
pub use decision::Decision;
pub use error::Error;
pub use error::Result;
pub use execpolicycheck::ExecPolicyCheckCommand;
pub use parser::PolicyParser;
pub use policy::Evaluation;
pub use policy::Policy;
+6 -6
View File
@@ -27,9 +27,9 @@ impl Policy {
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 {},
},
None => return Evaluation::NoMatch,
None => return Evaluation::NoMatch {},
};
let matched_rules: Vec<RuleMatch> =
@@ -39,7 +39,7 @@ impl Policy {
decision,
matched_rules,
},
None => Evaluation::NoMatch,
None => Evaluation::NoMatch {},
}
}
@@ -52,7 +52,7 @@ impl Policy {
.into_iter()
.flat_map(|command| match self.check(command.as_ref()) {
Evaluation::Match { matched_rules, .. } => matched_rules,
Evaluation::NoMatch => Vec::new(),
Evaluation::NoMatch { .. } => Vec::new(),
})
.collect();
@@ -61,7 +61,7 @@ impl Policy {
decision,
matched_rules,
},
None => Evaluation::NoMatch,
None => Evaluation::NoMatch {},
}
}
}
@@ -69,7 +69,7 @@ impl Policy {
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum Evaluation {
NoMatch,
NoMatch {},
Match {
decision: Decision,
#[serde(rename = "matchedRules")]
+10 -1
View File
@@ -10,6 +10,7 @@ use codex_execpolicy2::rule::PatternToken;
use codex_execpolicy2::rule::PrefixPattern;
use codex_execpolicy2::rule::PrefixRule;
use pretty_assertions::assert_eq;
use serde_json::json;
fn tokens(cmd: &[&str]) -> Vec<String> {
cmd.iter().map(std::string::ToString::to_string).collect()
@@ -60,6 +61,14 @@ prefix_rule(
);
}
#[test]
fn serializes_no_match_as_object() {
let serialized =
serde_json::to_value(&Evaluation::NoMatch {}).expect("should serialize evaluation");
assert_eq!(json!({"noMatch": {}}), serialized);
}
#[test]
fn parses_multiple_policy_files() {
let first_policy = r#"
@@ -288,7 +297,7 @@ prefix_rule(
"color.status=always",
"status",
]));
assert_eq!(Evaluation::NoMatch, no_match_eval);
assert_eq!(Evaluation::NoMatch {}, no_match_eval);
}
#[test]