1use fontoxide_misc::Tag;
4
5#[derive(Debug, thiserror::Error)]
10#[non_exhaustive]
11pub enum Error {
12 #[error("{0}")]
14 TtLib(String),
15 #[error("{0}")]
18 FileIsCollection(String),
19 #[error("'{0}' table not found")]
21 TableNotFound(Tag),
22 #[error("glyph not found: {0}")]
24 GlyphNotFound(String),
25 #[error("{0}")]
28 Struct(String),
29 #[error("assertion failed: {0}")]
31 Assertion(String),
32 #[error("{0}")]
34 Value(String),
35 #[error("error decompiling '{tag}' table: {source}")]
37 Decompile {
38 tag: Tag,
40 #[source]
42 source: Box<Error>,
43 },
44 #[error("error compiling '{tag}' table: {source}")]
46 Compile {
47 tag: Tag,
49 #[source]
51 source: Box<Error>,
52 },
53 #[error("TTX parse error: {0}")]
55 TtxParse(String),
56 #[error("'{0}' table is in use (circular table dependency)")]
59 TableBusy(Tag),
60 #[error("'{0}' table has an unexpected type")]
63 WrongTableType(Tag),
64 #[error("offset overflow: {0:?}")]
67 OffsetOverflow(Box<crate::ot::writer::OverflowErrorRecord>),
68 #[error(transparent)]
70 Io(#[from] std::io::Error),
71 #[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
95pub type Result<T, E = Error> = std::result::Result<T, E>;
97
98impl Error {
99 pub fn ttlib(msg: impl Into<String>) -> Self {
101 Error::TtLib(msg.into())
102 }
103
104 pub fn value(msg: impl Into<String>) -> Self {
106 Error::Value(msg.into())
107 }
108
109 pub fn ttx(msg: impl Into<String>) -> Self {
111 Error::TtxParse(msg.into())
112 }
113
114 pub fn into_pen_error(self) -> fontoxide_pens::Error {
116 fontoxide_pens::Error::Glyph(Box::new(self))
117 }
118
119 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}