allsorts_subset_browser/
error.rs

1//! Error types
2
3use crate::binary::read::ReadEof;
4use std::fmt;
5
6/// Error returned from font shaping functions
7#[derive(Clone, Eq, PartialEq, Debug)]
8pub enum ShapingError {
9    Indic(IndicError),
10    Parse(ParseError),
11}
12
13impl From<IndicError> for ShapingError {
14    fn from(error: IndicError) -> Self {
15        ShapingError::Indic(error)
16    }
17}
18
19impl From<ParseError> for ShapingError {
20    fn from(error: ParseError) -> Self {
21        ShapingError::Parse(error)
22    }
23}
24
25impl From<std::num::TryFromIntError> for ShapingError {
26    fn from(_error: std::num::TryFromIntError) -> Self {
27        ShapingError::Parse(ParseError::BadValue)
28    }
29}
30
31impl fmt::Display for ShapingError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            ShapingError::Indic(err) => write!(f, "indic shaping: {}", err),
35            ShapingError::Parse(err) => write!(f, "shaping parse: {}", err),
36        }
37    }
38}
39
40impl std::error::Error for ShapingError {}
41
42/// Error returned from font shaping Indic scripts
43#[derive(Clone, Eq, PartialEq, Debug)]
44pub enum IndicError {
45    EmptyBuffer,
46    MissingBaseConsonant,
47    MissingDottedCircle,
48    MissingTags,
49    UnexpectedGlyphOrigin,
50}
51
52/// Errors that originate when parsing binary data
53#[derive(Clone, Eq, PartialEq, Debug)]
54pub enum ParseError {
55    BadEof,
56    BadValue,
57    BadVersion,
58    BadOffset,
59    BadIndex,
60    LimitExceeded,
61    MissingValue,
62    CompressionError,
63    UnsuitableCmap,
64    NotImplemented,
65}
66
67impl From<ReadEof> for ParseError {
68    fn from(_error: ReadEof) -> Self {
69        ParseError::BadEof
70    }
71}
72
73impl From<std::num::TryFromIntError> for ParseError {
74    fn from(_error: std::num::TryFromIntError) -> Self {
75        ParseError::BadValue
76    }
77}
78
79impl fmt::Display for ParseError {
80    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
81        match self {
82            ParseError::BadEof => write!(f, "end of data reached unexpectedly"),
83            ParseError::BadValue => write!(f, "invalid value"),
84            ParseError::BadVersion => write!(f, "unexpected data version"),
85            ParseError::BadOffset => write!(f, "invalid data offset"),
86            ParseError::BadIndex => write!(f, "invalid data index"),
87            ParseError::LimitExceeded => write!(f, "limit exceeded"),
88            ParseError::MissingValue => write!(f, "an expected data value was missing"),
89            ParseError::CompressionError => write!(f, "compression error"),
90            ParseError::UnsuitableCmap => write!(f, "no suitable cmap subtable"),
91            ParseError::NotImplemented => write!(f, "feature not implemented"),
92        }
93    }
94}
95
96impl std::error::Error for ParseError {}
97
98impl fmt::Display for IndicError {
99    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100        match self {
101            IndicError::EmptyBuffer => write!(f, "empty buffer"),
102            IndicError::MissingBaseConsonant => write!(f, "missing base consonant"),
103            IndicError::MissingDottedCircle => write!(f, "missing dotted circle"),
104            IndicError::MissingTags => write!(f, "missing tags"),
105            IndicError::UnexpectedGlyphOrigin => write!(f, "unexpected glyph origin"),
106        }
107    }
108}
109
110impl std::error::Error for IndicError {}
111
112/// Errors that originate when writing binary data
113#[derive(Clone, Eq, PartialEq, Debug)]
114pub enum WriteError {
115    BadValue,
116    NotImplemented,
117    PlaceholderMismatch,
118}
119
120impl From<std::num::TryFromIntError> for WriteError {
121    fn from(_error: std::num::TryFromIntError) -> Self {
122        WriteError::BadValue
123    }
124}
125
126impl fmt::Display for WriteError {
127    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
128        match self {
129            WriteError::BadValue => write!(f, "write: bad value"),
130            WriteError::NotImplemented => write!(f, "writing in this format is not implemented"),
131            WriteError::PlaceholderMismatch => {
132                write!(f, "data written to placeholder did not match expected size")
133            }
134        }
135    }
136}
137
138impl std::error::Error for WriteError {}
139
140/// Enum that can hold read (`ParseError`) and write errors
141#[derive(Clone, Eq, PartialEq, Debug)]
142pub enum ReadWriteError {
143    Read(ParseError),
144    Write(WriteError),
145}
146
147impl From<ParseError> for ReadWriteError {
148    fn from(error: ParseError) -> Self {
149        ReadWriteError::Read(error)
150    }
151}
152
153impl From<WriteError> for ReadWriteError {
154    fn from(error: WriteError) -> Self {
155        ReadWriteError::Write(error)
156    }
157}
158
159impl fmt::Display for ReadWriteError {
160    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
161        match self {
162            ReadWriteError::Read(err) => write!(f, "read error: {}", err),
163            ReadWriteError::Write(err) => write!(f, "write error: {}", err),
164        }
165    }
166}
167
168impl std::error::Error for ReadWriteError {}