Skip to main content

gracile_core/
error.rs

1//! Error types and diagnostics for lexing, parsing, and rendering.
2
3use std::fmt;
4
5/// Source location (1-based line and column).
6#[derive(Debug, Clone)]
7pub struct Span {
8    pub line: u32,
9    pub col: u32,
10    pub offset: usize,
11}
12
13impl Span {
14    pub fn new(line: u32, col: u32, offset: usize) -> Self {
15        Span { line, col, offset }
16    }
17
18    pub fn unknown() -> Self {
19        Span {
20            line: 0,
21            col: 0,
22            offset: 0,
23        }
24    }
25}
26
27impl fmt::Display for Span {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        write!(f, "{}:{}", self.line, self.col)
30    }
31}
32
33/// All errors produced by Gracile.
34#[derive(Debug)]
35pub enum Error {
36    LexError { message: String, span: Span },
37    ParseError { message: String, span: Span },
38    RenderError { message: String },
39}
40
41impl fmt::Display for Error {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match self {
44            Error::LexError { message, span } => {
45                write!(f, "Lex error at {}: {}", span, message)
46            }
47            Error::ParseError { message, span } => {
48                write!(f, "Parse error at {}: {}", span, message)
49            }
50            Error::RenderError { message } => {
51                write!(f, "Render error: {}", message)
52            }
53        }
54    }
55}
56
57impl std::error::Error for Error {}
58
59pub type Result<T> = std::result::Result<T, Error>;