mirror of
https://github.com/openai/codex.git
synced 2026-05-01 11:52:10 +03:00
refactor: using deep assertions instead of expect tests; removed Display impls
This commit is contained in:
@@ -55,23 +55,3 @@ impl Evaluation {
|
||||
matches!(self, Self::Match { .. })
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Evaluation {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::NoMatch => f.write_str("noMatch"),
|
||||
Self::Match {
|
||||
decision,
|
||||
matched_rules,
|
||||
} => {
|
||||
writeln!(f, "match {{")?;
|
||||
writeln!(f, " decision: {decision},")?;
|
||||
writeln!(f, " matchedRules: [")?;
|
||||
for rule in matched_rules {
|
||||
writeln!(f, " {rule},")?;
|
||||
}
|
||||
write!(f, " ]\n}}")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,12 +29,6 @@ impl PatternToken {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for PatternToken {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.write_str(render_pattern_token(self).as_str())
|
||||
}
|
||||
}
|
||||
|
||||
/// Prefix matcher for commands with support for alternative match tokens.
|
||||
/// First token is fixed since we key by the first token in policy.
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
@@ -60,12 +54,6 @@ impl PrefixPattern {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for PrefixPattern {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "[{}]", render_pattern(self))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub enum RuleMatch {
|
||||
@@ -139,14 +127,14 @@ impl PrefixRule {
|
||||
|
||||
pub fn description(&self) -> String {
|
||||
format!(
|
||||
"prefix_rule(pattern = [{}], decision = {})",
|
||||
render_pattern(&self.pattern),
|
||||
render_decision(self.decision)
|
||||
"prefix_rule(pattern = {pattern:?}, decision = {decision})",
|
||||
pattern = self.pattern,
|
||||
decision = self.decision
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||
pub enum Rule {
|
||||
Prefix(PrefixRule),
|
||||
}
|
||||
@@ -175,40 +163,7 @@ impl Rule {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for Rule {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Prefix(rule) => write!(
|
||||
f,
|
||||
"prefix_rule(pattern = {}, decision = {})",
|
||||
rule.pattern, rule.decision
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn join_command(command: &[String]) -> String {
|
||||
try_join(command.iter().map(String::as_str))
|
||||
.unwrap_or_else(|_| "unable to render example".to_string())
|
||||
}
|
||||
|
||||
fn render_pattern(pattern: &PrefixPattern) -> String {
|
||||
let mut tokens = vec![pattern.first.as_ref().to_string()];
|
||||
tokens.extend(pattern.rest.iter().map(render_pattern_token));
|
||||
tokens.join(", ")
|
||||
}
|
||||
|
||||
fn render_pattern_token(token: &PatternToken) -> String {
|
||||
match token {
|
||||
PatternToken::Single(value) => value.clone(),
|
||||
PatternToken::Alts(values) => format!("[{}]", values.join(", ")),
|
||||
}
|
||||
}
|
||||
|
||||
fn render_decision(decision: Decision) -> &'static str {
|
||||
match decision {
|
||||
Decision::Allow => "allow",
|
||||
Decision::Prompt => "prompt",
|
||||
Decision::Forbidden => "forbidden",
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user