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
//! Error types for the Riegeli crate.
use std::borrow::Cow;
/// A decoded error from chunk operations.
#[derive(Debug)]
pub enum RiegeliError {
/// The chunk data hash did not match the stored hash in the header.
DataHashMismatch,
/// The chunk data is malformed or truncated.
MalformedData(Cow<'static, str>),
/// The compression type byte is not supported (feature not enabled or unknown).
UnsupportedCompression(u8),
/// The writer has been closed; no further writes are accepted.
WriterClosed,
/// A previous writer operation failed; the writer no longer accepts writes.
///
/// Once any write or flush fails, the underlying stream may hold a partial
/// chunk and the writer's position bookkeeping can no longer be trusted, so
/// the writer stays failed (matching the C++ implementation, where every
/// public entry point checks `ok()` first). The payload is the message of
/// the original error.
WriterFailed(Cow<'static, str>),
/// An I/O error occurred.
IoError(std::io::Error),
/// An unrecognized `ChunkType` byte was encountered.
UnknownChunkType(u8),
/// An unrecognized `CompressionType` byte was encountered.
UnknownCompressionType(u8),
}
impl std::fmt::Display for RiegeliError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RiegeliError::DataHashMismatch => {
write!(f, "chunk data hash mismatch: data is corrupted")
}
RiegeliError::MalformedData(msg) => write!(f, "malformed chunk data: {msg}"),
RiegeliError::UnsupportedCompression(byte) => {
write!(f, "unsupported compression type byte: {byte:#04x}")
}
RiegeliError::WriterClosed => write!(f, "writer has been closed"),
RiegeliError::WriterFailed(msg) => {
write!(f, "writer previously failed: {msg}")
}
RiegeliError::IoError(e) => write!(f, "I/O error: {e}"),
RiegeliError::UnknownChunkType(byte) => {
write!(f, "unknown chunk type byte: {byte:#04x}")
}
RiegeliError::UnknownCompressionType(byte) => {
write!(f, "unknown compression type byte: {byte:#04x}")
}
}
}
}
impl std::error::Error for RiegeliError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
RiegeliError::IoError(e) => Some(e),
_ => None,
}
}
}
impl From<std::io::Error> for RiegeliError {
fn from(e: std::io::Error) -> Self {
RiegeliError::IoError(e)
}
}