use std::char::DecodeUtf16Error;
use std::collections::TryReserveError;
use std::fmt;
use std::io;
use std::path::PathBuf;
use std::str::Utf8Error;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Format {
Bcd,
Bdict,
Qpyd,
Qcel,
Scel,
}
impl fmt::Display for Format {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Bcd => formatter.write_str("BCD"),
Self::Bdict => formatter.write_str("BDICT"),
Self::Qpyd => formatter.write_str("QPYD"),
Self::Qcel => formatter.write_str("QCEL"),
Self::Scel => formatter.write_str("SCEL"),
}
}
}
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum Error {
#[error("failed to read {format} dictionary `{path}`: {source}")]
Io {
format: Format,
path: PathBuf,
#[source]
source: io::Error,
},
#[error(
"unexpected end of {format} data at byte {offset:#x}: needed {needed} bytes, {available} available"
)]
UnexpectedEof {
format: Format,
offset: usize,
needed: usize,
available: usize,
},
#[error("invalid {format} magic: found {found:02x?}")]
InvalidMagic {
format: Format,
found: Vec<u8>,
},
#[error("unsupported {format} variant: found {found:02x?}")]
UnsupportedVariant {
format: Format,
found: Vec<u8>,
},
#[error("unsupported {format} version: found {found:02x?}")]
UnsupportedVersion {
format: Format,
found: Vec<u8>,
},
#[error("{field} in {format} data at byte {offset:#x} has odd UTF-16LE byte length {length}")]
OddUtf16ByteLength {
format: Format,
field: &'static str,
offset: usize,
length: usize,
},
#[error("invalid UTF-16LE in {format} field `{field}` at byte {offset:#x}: {source}")]
InvalidUtf16 {
format: Format,
field: &'static str,
offset: usize,
#[source]
source: DecodeUtf16Error,
},
#[error("invalid UTF-8 in {format} field `{field}` at byte {offset:#x}: {source}")]
InvalidUtf8 {
format: Format,
field: &'static str,
offset: usize,
#[source]
source: Utf8Error,
},
#[error("invalid ASCII byte {byte:#04x} in {format} field `{field}` at byte {offset:#x}")]
InvalidAscii {
format: Format,
field: &'static str,
offset: usize,
byte: u8,
},
#[error(
"failed to decompress {format} data at byte {offset:#x}: zlib returned status {status}"
)]
InvalidCompression {
format: Format,
offset: usize,
status: i32,
},
#[error(
"invalid {field} offset {offset:#x} in {format} data: expected {minimum:#x}..={maximum:#x}"
)]
InvalidOffset {
format: Format,
field: &'static str,
offset: usize,
minimum: usize,
maximum: usize,
},
#[error("{field} size mismatch in {format} data: expected {expected}, decoded {actual}")]
SizeMismatch {
format: Format,
field: &'static str,
expected: u64,
actual: u64,
},
#[error("failed to reserve {requested} items for {field} while parsing {format}: {source}")]
AllocationFailed {
format: Format,
field: &'static str,
requested: usize,
#[source]
source: TryReserveError,
},
#[error(
"invalid {field} count {count} in {format} data: at most {maximum} records fit in the remaining data"
)]
InvalidCount {
format: Format,
field: &'static str,
count: u64,
maximum: u64,
},
#[error("duplicate code index {index} in {format} data")]
DuplicateCodeIndex {
format: Format,
index: u64,
},
#[error("undefined code index {index} in {format} data at byte {offset:#x}")]
InvalidCodeIndex {
format: Format,
index: u64,
offset: usize,
},
#[error("invalid {field} index {index} in {format} data at byte {offset:#x}")]
InvalidCodeComponent {
format: Format,
field: &'static str,
index: u64,
offset: usize,
},
#[error("invalid {format} {field} record header at byte {offset:#x}: found {found:02x?}")]
InvalidRecordHeader {
format: Format,
field: &'static str,
offset: usize,
found: Vec<u8>,
},
#[error(
"invalid {field} length {length} in {format} data at byte {offset:#x}: at least {minimum} bytes are required"
)]
InvalidExtensionLength {
format: Format,
field: &'static str,
offset: usize,
length: usize,
minimum: usize,
},
#[error("{field} count mismatch in {format} data: expected {expected}, parsed {actual}")]
CountMismatch {
format: Format,
field: &'static str,
expected: u64,
actual: u64,
},
}
pub type Result<T> = std::result::Result<T, Error>;