Skip to main content

fontoxide_ttlib/
error.rs

1//! Error type.
2
3use fontoxide_misc::Tag;
4
5/// Errors from reading, writing or converting fonts.
6///
7/// The variants correspond to the exceptions fontTools raises, so that callers can
8/// tell them apart the same way.
9#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12	/// `TTLibError`: malformed or unsupported font data, or a misuse of the API.
13	#[error("{0}")]
14	TtLib(String),
15	/// `TTLibFileIsCollectionError`: a collection was opened without a valid font
16	/// number.
17	#[error("{0}")]
18	FileIsCollection(String),
19	/// `KeyError` for a missing table.
20	#[error("'{0}' table not found")]
21	TableNotFound(Tag),
22	/// `KeyError` for an unknown glyph name.
23	#[error("glyph not found: {0}")]
24	GlyphNotFound(String),
25	/// `struct.error`: data too short to unpack, or a value that does not fit its
26	/// binary field.
27	#[error("{0}")]
28	Struct(String),
29	/// `AssertionError` raised by fontTools on inconsistent data.
30	#[error("assertion failed: {0}")]
31	Assertion(String),
32	/// `ValueError` / `TypeError` for invalid values.
33	#[error("{0}")]
34	Value(String),
35	/// An error while decompiling a table, with the table it occurred in.
36	#[error("error decompiling '{tag}' table: {source}")]
37	Decompile {
38		/// The table being decompiled.
39		tag: Tag,
40		/// The underlying error.
41		#[source]
42		source: Box<Error>,
43	},
44	/// An error while compiling a table, with the table it occurred in.
45	#[error("error compiling '{tag}' table: {source}")]
46	Compile {
47		/// The table being compiled.
48		tag: Tag,
49		/// The underlying error.
50		#[source]
51		source: Box<Error>,
52	},
53	/// `TTXParseError` or an XML syntax error.
54	#[error("TTX parse error: {0}")]
55	TtxParse(String),
56	/// A required table is currently being decompiled, compiled or dumped, so it
57	/// cannot be accessed (a circular dependency between tables).
58	#[error("'{0}' table is in use (circular table dependency)")]
59	TableBusy(Tag),
60	/// A table exists but is not of the requested type (for example, it could not
61	/// be decompiled and is kept as raw data).
62	#[error("'{0}' table has an unexpected type")]
63	WrongTableType(Tag),
64	/// `OTLOffsetOverflowError`: a 16-bit offset does not fit while compiling an
65	/// OpenType Layout table.
66	#[error("offset overflow: {0:?}")]
67	OffsetOverflow(Box<crate::ot::writer::OverflowErrorRecord>),
68	/// An I/O error.
69	#[error(transparent)]
70	Io(#[from] std::io::Error),
71	/// `ImportError`: the functionality needs an optional dependency of
72	/// fontTools (one of its extras), which the Cargo feature of the same name
73	/// leaves out; the message is fontTools' (for example
74	/// `No module named brotli`).
75	#[error("{0}")]
76	Import(String),
77}
78
79impl From<fontoxide_pens::Error> for Error {
80	fn from(e: fontoxide_pens::Error) -> Self {
81		match e {
82			fontoxide_pens::Error::Glyph(inner) => match inner.downcast::<Error>() {
83				Ok(e) => *e,
84				Err(other) => Error::TtLib(other.to_string()),
85			},
86			fontoxide_pens::Error::MissingComponent(name) => {
87				Error::TtLib(format!("MissingComponentError: {name}"))
88			}
89			fontoxide_pens::Error::Usage(msg) => Error::TtLib(msg),
90			fontoxide_pens::Error::Pen(msg) => Error::TtLib(format!("PenError: {msg}")),
91		}
92	}
93}
94
95/// Shorthand for `Result<T, fontoxide_ttlib::Error>`.
96pub type Result<T, E = Error> = std::result::Result<T, E>;
97
98impl Error {
99	/// A `TTLibError` with the given message.
100	pub fn ttlib(msg: impl Into<String>) -> Self {
101		Error::TtLib(msg.into())
102	}
103
104	/// A `ValueError` with the given message.
105	pub fn value(msg: impl Into<String>) -> Self {
106		Error::Value(msg.into())
107	}
108
109	/// A TTX parse error with the given message.
110	pub fn ttx(msg: impl Into<String>) -> Self {
111		Error::TtxParse(msg.into())
112	}
113
114	/// This error as one of [`fontoxide_pens`], to pass it through a pen.
115	pub fn into_pen_error(self) -> fontoxide_pens::Error {
116		fontoxide_pens::Error::Glyph(Box::new(self))
117	}
118
119	/// The innermost error, skipping the `Decompile`/`Compile` wrappers.
120	pub fn root_cause(&self) -> &Error {
121		match self {
122			Error::Decompile { source, .. } | Error::Compile { source, .. } => source.root_cause(),
123			other => other,
124		}
125	}
126}