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}
17
18impl std::fmt::Display for LexError {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            LexError::UnclosedTag => write!(f, "unclosed tag"),
22            LexError::InvalidTag(tag) => write!(f, "invalid tag: {tag}"),
23            LexError::UnclosedValue => write!(f, "unclosed parantheses for color value"),
24            LexError::InvalidArgumentCount { expected, got } => {
25                write!(f, "expected {}, got {}", expected, got)
26            }
27            LexError::InvalidValue(s) => write!(f, "invalid value '{}'", s),
28        }
29    }
30}
31
32impl std::error::Error for LexError {}