rich/errors.rs
1//! Error types.
2//!
3//! Port of the exceptions in upstream `rich/errors.py`. As more of the library
4//! is ported, additional variants are added here rather than scattering ad-hoc
5//! error types across modules.
6
7use std::fmt;
8
9/// Errors produced while parsing colors, styles, or markup.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum RichError {
12 /// A color could not be parsed (`rich.errors.ColorParseError`).
13 ColorParse(String),
14 /// A style definition could not be parsed (`rich.errors.StyleSyntaxError`).
15 StyleSyntax(String),
16 /// Console markup was malformed (`rich.errors.MarkupError`).
17 Markup(String),
18 /// A JSON string could not be parsed (used by `rich.json.JSON`).
19 Json(String),
20 /// A regular expression could not be compiled or run. Python has no rich
21 /// equivalent — it lets `re.error` propagate — but a caller-supplied pattern
22 /// has to fail somewhere, and returning it beats panicking.
23 Regex(String),
24}
25
26impl fmt::Display for RichError {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 match self {
29 RichError::ColorParse(msg) => write!(f, "color parse error: {msg}"),
30 RichError::StyleSyntax(msg) => write!(f, "style syntax error: {msg}"),
31 RichError::Markup(msg) => write!(f, "markup error: {msg}"),
32 RichError::Json(msg) => write!(f, "json parse error: {msg}"),
33 RichError::Regex(msg) => write!(f, "regex error: {msg}"),
34 }
35 }
36}
37
38impl std::error::Error for RichError {}
39
40/// Convenience alias mirroring the subset of `rich` that raises on bad input.
41pub type Result<T> = std::result::Result<T, RichError>;