sdf_text/error.rs
1//! The crate's error type.
2//!
3//! One enum for `sdf-text`, as issue #9 asks for. The JS this ports throws from
4//! `opentype.parse`; here that is the [`Error::Parse`] variant, and the rest of
5//! the crate's surface is infallible — layout, rasterisation and the atlas take
6//! a font that has already parsed.
7
8use std::fmt;
9
10/// Everything the public API of `sdf-text` can fail with.
11///
12/// Non-exhaustive: more of the crate's surface may become fallible as the port
13/// grows, and matching code should carry a `_` arm.
14#[derive(Debug)]
15#[non_exhaustive]
16pub enum Error {
17 /// Font bytes `ttf-parser` could not read a face out of. `src` is the
18 /// label the caller passed to [`crate::VectorFont::parse`].
19 Parse {
20 /// The label the caller passed to [`crate::VectorFont::parse`].
21 src: String,
22 /// The underlying `ttf-parser` failure.
23 source: owned_ttf_parser::FaceParsingError,
24 },
25}
26
27impl fmt::Display for Error {
28 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29 match self {
30 Self::Parse { src, source } => {
31 write!(f, "VectorFont: cannot parse the font {src}: {source}")
32 }
33 }
34 }
35}
36
37impl std::error::Error for Error {
38 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
39 match self {
40 Self::Parse { source, .. } => Some(source),
41 }
42 }
43}