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
use std::fmt;
/// Result type returned by `mdictlib` operations.
pub type Result<T> = std::result::Result<T, Error>;
/// An error reported while configuring, opening, or reading an MDict file.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// An underlying file or destination I/O operation failed.
Io(std::io::Error),
/// The file violates a fixed structural invariant.
InvalidFormat(&'static str),
/// File-derived data is internally inconsistent or malformed.
InvalidData(String),
/// A bounded input ended before a required field could be read.
Truncated {
/// Description of the field or section being read.
context: &'static str,
/// Number of bytes required by the operation.
needed: usize,
/// Number of bytes that remained in the bounded input.
remaining: usize,
},
/// A file-derived value exceeded a configured safety ceiling.
LimitExceeded {
/// Stable name of the limit that was exceeded.
limit: &'static str,
/// Value requested or declared by the file.
value: u64,
/// Maximum value accepted by the reader.
max: u64,
},
/// A bounded allocation could not be reserved without panicking.
AllocationFailed {
/// Description of the allocation being attempted.
context: &'static str,
/// Total number of bytes requested from the allocator, when known.
requested: u64,
},
/// A section or block checksum did not match its payload.
ChecksumMismatch {
/// Description of the section or block being verified.
context: &'static str,
/// Checksum declared by the file.
expected: u32,
/// Checksum calculated from the payload.
actual: u32,
},
/// Text bytes could not be decoded with the declared encoding.
Decode {
/// Description of the text field being decoded.
context: &'static str,
/// Canonical name of the attempted encoding.
encoding: &'static str,
},
/// An encrypted dictionary requires passcode material that was not supplied.
MissingPasscode,
/// Caller-supplied passcode material is malformed.
InvalidPasscode(&'static str),
/// The file requests a format feature that this build does not support.
Unsupported(&'static str),
}
impl Error {
pub(crate) fn truncated(context: &'static str, needed: usize, remaining: usize) -> Self {
Self::Truncated {
context,
needed,
remaining,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(error) => write!(f, "I/O error: {error}"),
Self::InvalidFormat(context) => write!(f, "invalid format: {context}"),
Self::InvalidData(context) => write!(f, "invalid data: {context}"),
Self::Truncated {
context,
needed,
remaining,
} => write!(
f,
"truncated {context}: need {needed} bytes, have {remaining}"
),
Self::LimitExceeded { limit, value, max } => {
write!(f, "limit exceeded for {limit}: {value} > {max}")
}
Self::AllocationFailed { context, requested } => {
write!(f, "failed to reserve {requested} bytes for {context}")
}
Self::ChecksumMismatch {
context,
expected,
actual,
} => write!(
f,
"checksum mismatch for {context}: expected {expected:#010x}, got {actual:#010x}"
),
Self::Decode { context, encoding } => {
write!(f, "failed to decode {context} using {encoding}")
}
Self::MissingPasscode => write!(f, "dictionary requires a passcode"),
Self::InvalidPasscode(context) => write!(f, "invalid passcode: {context}"),
Self::Unsupported(feature) => write!(f, "unsupported feature: {feature}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(error) => Some(error),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Self::Io(value)
}
}