latex_rust/error.rs
1//! Public error types. Unsupported input is never rendered.
2
3use core::fmt;
4
5/// Crate-level error.
6///
7/// Unsupported input is never rendered. [`Error::Parse`] wraps tokenizer/parser
8/// failures; [`Error::Font`] wraps face/glyph failures; [`Error::Unsupported`]
9/// is a construct or feature out of scope; [`Error::Malformed`] is a bad value
10/// that parsed as the wrong shape; [`Error::InvalidOption`] is a render option
11/// out of range.
12#[derive(Clone, Debug, PartialEq, Eq)]
13pub enum Error {
14 /// Tokenizer or parser rejected the input.
15 Parse(ParseError),
16 /// Font bytes or a requested glyph could not be used.
17 Font(FontError),
18 /// A feature listed as out of scope, or not yet implemented.
19 ///
20 /// Callers must treat this as failure. The renderer does not invent output.
21 Unsupported {
22 /// Human-readable name of the missing feature or construct.
23 what: String,
24 },
25 /// Syntactically invalid value (for example a color spec with the wrong
26 /// number of components). Distinct from [`Self::Unsupported`].
27 Malformed {
28 /// Human-readable description of what was malformed.
29 what: String,
30 },
31 /// A render option is out of range (for example PNG DPI of 0 or above 2400).
32 InvalidOption {
33 /// Human-readable name of the invalid option.
34 what: String,
35 },
36}
37
38/// Tokenizer / parser failure.
39///
40/// The enum is non-exhaustive so a later release can add a variant. A
41/// downstream match needs a wildcard.
42#[derive(Clone, Debug, PartialEq, Eq)]
43#[non_exhaustive]
44pub enum ParseError {
45 /// Input ended with a stray `\` and no command character.
46 TrailingBackslash,
47 /// Command is known but this crate will not invent a rendering for it.
48 Unsupported(String),
49 /// Command is not in the catalog and is not a known math structure.
50 Unknown(String),
51 /// Syntactically invalid input. Names the construct or position.
52 Malformed(String),
53 /// `\left` without `\right`, or `\right` without `\left`.
54 UnmatchedDelimiter,
55 /// `\\` or `\cr` outside a matrix, array, cases, or substack.
56 RowBreakOutside,
57}
58
59/// Font loader or metric lookup failure.
60#[derive(Clone, Debug, PartialEq, Eq)]
61pub enum FontError {
62 /// Embedded or supplied bytes are not a usable OpenType face.
63 InvalidFace,
64 /// Character has no glyph in this face.
65 MissingGlyph {
66 /// Requested character.
67 ch: char,
68 },
69}
70
71impl fmt::Display for Error {
72 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
73 match self {
74 Self::Parse(e) => write!(f, "{e}"),
75 Self::Font(e) => write!(f, "{e}"),
76 Self::Unsupported { what } => write!(f, "unsupported: {what}"),
77 Self::Malformed { what } => write!(f, "malformed: {what}"),
78 Self::InvalidOption { what } => write!(f, "invalid option: {what}"),
79 }
80 }
81}
82
83impl fmt::Display for ParseError {
84 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85 match self {
86 Self::TrailingBackslash => f.write_str("trailing backslash"),
87 Self::Unsupported(s) => write!(f, "unsupported: {s}"),
88 Self::Unknown(s) => write!(f, "unknown command: {s}"),
89 Self::Malformed(s) => write!(f, "malformed: {s}"),
90 Self::UnmatchedDelimiter => f.write_str("unmatched delimiter"),
91 Self::RowBreakOutside => f.write_str(r"row break \\ outside an environment"),
92 }
93 }
94}
95
96impl fmt::Display for FontError {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 match self {
99 Self::InvalidFace => f.write_str("invalid OpenType face"),
100 Self::MissingGlyph { ch } => write!(f, "missing glyph for {ch:?}"),
101 }
102 }
103}
104
105impl std::error::Error for Error {}
106
107impl From<ParseError> for Error {
108 fn from(e: ParseError) -> Self {
109 Self::Parse(e)
110 }
111}
112
113impl From<FontError> for Error {
114 fn from(e: FontError) -> Self {
115 Self::Font(e)
116 }
117}