mirror of
https://github.com/openai/codex.git
synced 2026-11-21 22:50:37 +00:00
## Why Configuration, protocol, and prompt code use in-memory policies and previously pulled in the Starlark parser through `codex-execpolicy`. ## What changed Move policy construction, rule validation, and evaluation into `codex-execpolicy-rules` and use it directly from those crates. Keep Starlark policy loading and file updates in `codex-execpolicy`, which re-exports the shared rule types and wraps rule validation errors while preserving their source locations. GitOrigin-RevId: 5bc70d3cd387b43a3ad1fcc56af96ad169ac8b13
85 lines
2.2 KiB
Rust
85 lines
2.2 KiB
Rust
//! Rule validation failures and their optional locations in the source policy.
|
|
|
|
use thiserror::Error;
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub struct TextPosition {
|
|
pub line: usize,
|
|
pub column: usize,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub struct TextRange {
|
|
pub start: TextPosition,
|
|
pub end: TextPosition,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct ErrorLocation {
|
|
pub path: String,
|
|
pub range: TextRange,
|
|
}
|
|
|
|
#[derive(Debug, Error)]
|
|
pub enum Error {
|
|
#[error("invalid decision: {0}")]
|
|
InvalidDecision(String),
|
|
#[error("invalid pattern element: {0}")]
|
|
InvalidPattern(String),
|
|
#[error("invalid example: {0}")]
|
|
InvalidExample(String),
|
|
#[error("invalid rule: {0}")]
|
|
InvalidRule(String),
|
|
#[error(
|
|
"expected every example to match at least one rule. rules: {rules:?}; unmatched examples: \
|
|
{examples:?}"
|
|
)]
|
|
ExampleDidNotMatch {
|
|
rules: Vec<String>,
|
|
examples: Vec<String>,
|
|
location: Option<ErrorLocation>,
|
|
},
|
|
#[error("expected example to not match rule `{rule}`: {example}")]
|
|
ExampleDidMatch {
|
|
rule: String,
|
|
example: String,
|
|
location: Option<ErrorLocation>,
|
|
},
|
|
}
|
|
|
|
impl Error {
|
|
pub fn with_location(self, location: ErrorLocation) -> Self {
|
|
match self {
|
|
Error::ExampleDidNotMatch {
|
|
rules,
|
|
examples,
|
|
location: None,
|
|
} => Error::ExampleDidNotMatch {
|
|
rules,
|
|
examples,
|
|
location: Some(location),
|
|
},
|
|
Error::ExampleDidMatch {
|
|
rule,
|
|
example,
|
|
location: None,
|
|
} => Error::ExampleDidMatch {
|
|
rule,
|
|
example,
|
|
location: Some(location),
|
|
},
|
|
other => other,
|
|
}
|
|
}
|
|
|
|
pub fn location(&self) -> Option<ErrorLocation> {
|
|
match self {
|
|
Error::ExampleDidNotMatch { location, .. }
|
|
| Error::ExampleDidMatch { location, .. } => location.clone(),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|