Skip to main content

farben_core/
errors.rs

1/// Errors produced during tokenization of a farben markup string.
2#[derive(Debug)]
3pub enum LexError {
4    /// A `[` was found with no matching `]`.
5    UnclosedTag,
6    /// The tag name is not a recognized keyword or color form.
7    InvalidTag(String),
8    /// A color value function (e.g. `rgb(` or `ansi(`) was opened but never closed.
9    UnclosedValue,
10    /// A color function received the wrong number of arguments.
11    InvalidArgumentCount { expected: usize, got: usize },
12    /// An argument could not be parsed into the expected numeric type.
13    InvalidValue(String),
14}
15
16impl std::fmt::Display for LexError {
17    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
18        match self {
19            LexError::UnclosedTag => write!(f, "unclosed tag"),
20            LexError::InvalidTag(tag) => write!(f, "invalid tag: {tag}"),
21            LexError::UnclosedValue => write!(f, "unclosed parantheses for color value"),
22            LexError::InvalidArgumentCount { expected, got } => {
23                write!(f, "expected {}, got {}", expected, got)
24            }
25            LexError::InvalidValue(s) => write!(f, "invalid value '{}'", s),
26        }
27    }
28}
29
30impl std::error::Error for LexError {}