2025-11-19 19:14:10 -08:00
|
|
|
use starlark::Error as StarlarkError;
|
|
|
|
|
use thiserror::Error;
|
2025-04-24 17:14:47 -07:00
|
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
2026-01-23 20:11:09 -08:00
|
|
|
#[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,
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-19 19:14:10 -08:00
|
|
|
#[derive(Debug, Error)]
|
2025-04-24 17:14:47 -07:00
|
|
|
pub enum Error {
|
2025-11-19 19:14:10 -08:00
|
|
|
#[error("invalid decision: {0}")]
|
|
|
|
|
InvalidDecision(String),
|
|
|
|
|
#[error("invalid pattern element: {0}")]
|
|
|
|
|
InvalidPattern(String),
|
|
|
|
|
#[error("invalid example: {0}")]
|
|
|
|
|
InvalidExample(String),
|
2026-01-05 13:24:48 -08:00
|
|
|
#[error("invalid rule: {0}")]
|
|
|
|
|
InvalidRule(String),
|
2025-11-19 19:14:10 -08:00
|
|
|
#[error(
|
|
|
|
|
"expected every example to match at least one rule. rules: {rules:?}; unmatched examples: \
|
|
|
|
|
{examples:?}"
|
|
|
|
|
)]
|
|
|
|
|
ExampleDidNotMatch {
|
|
|
|
|
rules: Vec<String>,
|
|
|
|
|
examples: Vec<String>,
|
|
|
|
|
},
|
|
|
|
|
#[error("expected example to not match rule `{rule}`: {example}")]
|
|
|
|
|
ExampleDidMatch { rule: String, example: String },
|
|
|
|
|
#[error("starlark error: {0}")]
|
|
|
|
|
Starlark(StarlarkError),
|
2025-04-24 17:14:47 -07:00
|
|
|
}
|
2026-01-23 20:11:09 -08:00
|
|
|
|
|
|
|
|
impl Error {
|
|
|
|
|
pub fn location(&self) -> Option<ErrorLocation> {
|
|
|
|
|
match self {
|
|
|
|
|
Error::Starlark(err) => err.span().map(|span| {
|
|
|
|
|
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,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|