Skip to main content

farben_core/
errors.rs

1//! Error types produced during farben markup tokenization.
2
3/// Errors produced during tokenization of a farben markup string.
4#[derive(Debug)]
5pub enum LexError {
6    /// A `[` was found with no matching `]`.
7    UnclosedTag,
8    /// The tag name is not a recognized keyword or color form.
9    InvalidTag(String),
10    /// A color value function (e.g. `rgb(` or `ansi(`) was opened but never closed.
11    UnclosedValue,
12    /// A color function received the wrong number of arguments.
13    InvalidArgumentCount { expected: usize, got: usize },
14    /// An argument could not be parsed into the expected numeric type.
15    InvalidValue(String),
16    /// A reset tag was given a target that cannot be reset (e.g. `[/reset]` or `[/prefix]`).
17    InvalidResetTarget,
18    /// The `prefix!` macro was called with a style name that has not been registered.
19    UnknownStyle(String),
20}
21
22impl std::fmt::Display for LexError {
23    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
24        match self {
25            LexError::UnclosedTag => write!(f, "unclosed tag"),
26            LexError::InvalidTag(tag) => write!(f, "invalid tag: {tag}"),
27            LexError::UnclosedValue => write!(f, "unclosed parentheses for color value"),
28            LexError::InvalidArgumentCount { expected, got } => {
29                write!(f, "expected {expected} arguments, got {got}")
30            }
31            LexError::InvalidValue(s) => write!(f, "invalid value '{s}'"),
32            LexError::InvalidResetTarget => {
33                write!(f, "reset target must be a color or emphasis tag")
34            }
35            LexError::UnknownStyle(name) => {
36                write!(f, "no style named '{name}' in the registry")
37            }
38        }
39    }
40}
41
42impl std::error::Error for LexError {}