#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OpyError {
pub code: String,
pub message: String,
pub span: Option<Span>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Span {
pub file: u32,
pub start: Position,
pub end: Position,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize)]
pub struct Position {
pub line: u32,
pub col: u32,
}
impl Position {
pub const fn new(line: u32, col: u32) -> Position {
Position { line, col }
}
}
impl Span {
pub fn new(file: u32, start: Position, end: Position) -> Span {
Span { file, start, end }
}
}
pub type OpyResult<T> = Result<T, OpyError>;
impl OpyError {
pub fn new(code: impl Into<String>, message: impl Into<String>) -> OpyError {
OpyError {
code: code.into(),
message: message.into(),
span: None,
}
}
pub fn at(code: impl Into<String>, message: impl Into<String>, span: Span) -> OpyError {
OpyError {
code: code.into(),
message: message.into(),
span: Some(span),
}
}
}
impl std::fmt::Display for OpyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}: {}", self.code, self.message)
}
}
impl std::error::Error for OpyError {}