logo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//! Error types

use crate::binary::read::ReadEof;
use std::fmt;

/// Error returned from font shaping functions
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum ShapingError {
    Indic(IndicError),
    Parse(ParseError),
}

impl From<IndicError> for ShapingError {
    fn from(error: IndicError) -> Self {
        ShapingError::Indic(error)
    }
}

impl From<ParseError> for ShapingError {
    fn from(error: ParseError) -> Self {
        ShapingError::Parse(error)
    }
}

impl From<std::num::TryFromIntError> for ShapingError {
    fn from(_error: std::num::TryFromIntError) -> Self {
        ShapingError::Parse(ParseError::BadValue)
    }
}

impl fmt::Display for ShapingError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ShapingError::Indic(err) => write!(f, "indic shaping: {}", err),
            ShapingError::Parse(err) => write!(f, "shaping parse: {}", err),
        }
    }
}

impl std::error::Error for ShapingError {}

/// Error returned from font shaping Indic scripts
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum IndicError {
    EmptyBuffer,
    MissingBaseConsonant,
    MissingDottedCircle,
    MissingTags,
    UnexpectedGlyphOrigin,
}

/// Errors that originate when parsing binary data
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum ParseError {
    BadEof,
    BadValue,
    BadVersion,
    BadOffset,
    BadIndex,
    LimitExceeded,
    MissingValue,
    CompressionError,
    NotImplemented,
}

impl From<ReadEof> for ParseError {
    fn from(_error: ReadEof) -> Self {
        ParseError::BadEof
    }
}

impl From<std::num::TryFromIntError> for ParseError {
    fn from(_error: std::num::TryFromIntError) -> Self {
        ParseError::BadValue
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ParseError::BadEof => write!(f, "end of data reached unexpectedly"),
            ParseError::BadValue => write!(f, "invalid value"),
            ParseError::BadVersion => write!(f, "unexpected data version"),
            ParseError::BadOffset => write!(f, "invalid data offset"),
            ParseError::BadIndex => write!(f, "invalid data index"),
            ParseError::LimitExceeded => write!(f, "limit exceeded"),
            ParseError::MissingValue => write!(f, "an expected data value was missing"),
            ParseError::CompressionError => write!(f, "compression error"),
            ParseError::NotImplemented => write!(f, "feature not implemented"),
        }
    }
}

impl std::error::Error for ParseError {}

impl fmt::Display for IndicError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            IndicError::EmptyBuffer => write!(f, "empty buffer"),
            IndicError::MissingBaseConsonant => write!(f, "missing base consonant"),
            IndicError::MissingDottedCircle => write!(f, "missing dotted circle"),
            IndicError::MissingTags => write!(f, "missing tags"),
            IndicError::UnexpectedGlyphOrigin => write!(f, "unexpected glyph origin"),
        }
    }
}

impl std::error::Error for IndicError {}

/// Errors that originate when writing binary data
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum WriteError {
    BadValue,
    NotImplemented,
}

impl From<std::num::TryFromIntError> for WriteError {
    fn from(_error: std::num::TryFromIntError) -> Self {
        WriteError::BadValue
    }
}

impl fmt::Display for WriteError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            WriteError::BadValue => write!(f, "write: bad value"),
            WriteError::NotImplemented => write!(f, "writing in this format is not implemented"),
        }
    }
}

impl std::error::Error for WriteError {}

/// Enum that can hold read (`ParseError`) and write errors
#[derive(Clone, Eq, PartialEq, Debug)]
pub enum ReadWriteError {
    Read(ParseError),
    Write(WriteError),
}

impl From<ParseError> for ReadWriteError {
    fn from(error: ParseError) -> Self {
        ReadWriteError::Read(error)
    }
}

impl From<WriteError> for ReadWriteError {
    fn from(error: WriteError) -> Self {
        ReadWriteError::Write(error)
    }
}

impl fmt::Display for ReadWriteError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ReadWriteError::Read(err) => write!(f, "read error: {}", err),
            ReadWriteError::Write(err) => write!(f, "write error: {}", err),
        }
    }
}

impl std::error::Error for ReadWriteError {}