crates/highlight/src/error.rs 1.3 KiB raw
1
//! Error types for grammar, theme, and registry operations.
2
//!
3
//! The crate intentionally exposes a small error surface: invalid source data,
4
//! invalid regexes, and failed registry lookups. Lower-level parser details are
5
//! collapsed here so callers can report actionable load or selection failures
6
//! without depending on the JSON or regex implementation.
7
8
use std::fmt;
9
10
/// Highlighting error.
11
#[derive(Clone, Debug, Eq, PartialEq)]
12
pub enum Error {
13
    /// Input could not be parsed as a valid grammar.
14
    InvalidGrammar,
15
    /// Input could not be parsed as a valid theme.
16
    InvalidTheme,
17
    /// Regex data could not be compiled.
18
    InvalidRegex(String),
19
    /// A named grammar was not found.
20
    UnknownSyntax(String),
21
    /// A named theme was not found.
22
    UnknownTheme(String),
23
}
24
25
impl fmt::Display for Error {
26
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27
        match self {
28
            Self::InvalidGrammar => f.write_str("invalid grammar"),
29
            Self::InvalidTheme => f.write_str("invalid theme"),
30
            Self::InvalidRegex(pattern) => write!(f, "invalid regex `{pattern}`"),
31
            Self::UnknownSyntax(name) => write!(f, "unknown syntax `{name}`"),
32
            Self::UnknownTheme(name) => write!(f, "unknown theme `{name}`"),
33
        }
34
    }
35
}
36
37
impl std::error::Error for Error {}