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
48 lines
1.4 KiB
Rust
48 lines
1.4 KiB
Rust
//! Diagnostics for loading and validating a Starlark policy file.
|
|
|
|
use crate::ErrorLocation;
|
|
use crate::RuleError;
|
|
use codex_execpolicy_rules::TextPosition;
|
|
use codex_execpolicy_rules::TextRange;
|
|
use starlark::codemap::FileSpan;
|
|
use thiserror::Error;
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
/// A failure to evaluate a policy file or validate its rules.
|
|
#[derive(Debug, Error)]
|
|
pub enum Error {
|
|
#[error("starlark error: {0}")]
|
|
Starlark(starlark::Error),
|
|
#[error(transparent)]
|
|
Rule(#[from] RuleError),
|
|
}
|
|
|
|
impl Error {
|
|
/// Return the one-based source range when the failure has a span.
|
|
pub fn location(&self) -> Option<ErrorLocation> {
|
|
match self {
|
|
Error::Starlark(error) => error.span().map(error_location_from_file_span),
|
|
Error::Rule(error) => error.location(),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Convert a parser span to the one-based coordinates used for policy diagnostics.
|
|
pub(crate) fn error_location_from_file_span(span: &FileSpan) -> ErrorLocation {
|
|
let resolved = span.resolve_span();
|
|
ErrorLocation {
|
|
path: span.filename().to_string(),
|
|
range: TextRange {
|
|
start: TextPosition {
|
|
line: resolved.begin.line + 1,
|
|
column: resolved.begin.column + 1,
|
|
},
|
|
end: TextPosition {
|
|
line: resolved.end.line + 1,
|
|
column: resolved.end.column + 1,
|
|
},
|
|
},
|
|
}
|
|
}
|