Skip to main content

rich_rs/
error.rs

1//! Error types for rich-rs.
2
3use thiserror::Error;
4
5/// Errors that can occur when parsing styles, colors, or markup.
6#[derive(Debug, Clone, PartialEq, Eq, Error)]
7#[non_exhaustive]
8pub enum ParseError {
9    /// Invalid color specification.
10    #[error("invalid color: {0}")]
11    InvalidColor(String),
12
13    /// Invalid style specification.
14    #[error("invalid style: {0}")]
15    InvalidStyle(String),
16
17    /// Invalid markup syntax.
18    #[error("invalid markup: {0}")]
19    InvalidMarkup(String),
20
21    /// Unclosed tag in markup.
22    #[error("unclosed tag: {0}")]
23    UnclosedTag(String),
24
25    /// Unexpected closing tag in markup.
26    #[error("unexpected closing tag: {0}")]
27    UnexpectedClosingTag(String),
28
29    /// No emoji found with the given name.
30    #[error("no emoji called {0:?}")]
31    NoEmoji(String),
32}
33
34impl ParseError {
35    /// Create an invalid color error.
36    pub fn invalid_color(s: impl Into<String>) -> Self {
37        ParseError::InvalidColor(s.into())
38    }
39
40    /// Create an invalid style error.
41    pub fn invalid_style(s: impl Into<String>) -> Self {
42        ParseError::InvalidStyle(s.into())
43    }
44
45    /// Create an invalid markup error.
46    pub fn invalid_markup(s: impl Into<String>) -> Self {
47        ParseError::InvalidMarkup(s.into())
48    }
49
50    /// Create a no emoji error.
51    pub fn no_emoji(s: impl Into<String>) -> Self {
52        ParseError::NoEmoji(s.into())
53    }
54}
55
56/// A specialized Result type for parsing operations.
57pub type Result<T> = std::result::Result<T, ParseError>;