1use crate::{Checksum, CompressionType};
6
7#[derive(Debug)]
9#[non_exhaustive]
10pub enum Error {
11 Io(std::io::Error),
13
14 Decompress(CompressionType),
16
17 InvalidVersion(u8),
19
20 Unrecoverable,
22
23 ChecksumMismatch {
25 got: Checksum,
27
28 expected: Checksum,
30 },
31
32 InvalidTag((&'static str, u8)),
34
35 InvalidTrailer,
37
38 InvalidHeader(&'static str),
40
41 Utf8(std::str::Utf8Error),
43}
44
45#[cfg_attr(test, mutants::skip)]
46impl std::fmt::Display for Error {
47 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48 write!(f, "LsmTreeError: {self:?}")
49 }
50}
51
52impl std::error::Error for Error {
53 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
54 match self {
55 Self::Io(e) => Some(e),
56 _ => None,
57 }
58 }
59}
60
61impl From<sfa::Error> for Error {
62 fn from(value: sfa::Error) -> Self {
63 match value {
64 sfa::Error::Io(e) => Self::from(e),
65 sfa::Error::ChecksumMismatch { got, expected } => {
66 log::error!("Archive ToC checksum mismatch");
67 Self::ChecksumMismatch {
68 got: got.into(),
69 expected: expected.into(),
70 }
71 }
72 sfa::Error::InvalidHeader => {
73 log::error!("Invalid archive header");
74 Self::Unrecoverable
75 }
76 sfa::Error::InvalidVersion => {
77 log::error!("Invalid archive version");
78 Self::Unrecoverable
79 }
80 sfa::Error::UnsupportedChecksumType => {
81 log::error!("Invalid archive checksum type");
82 Self::Unrecoverable
83 }
84 }
85 }
86}
87
88impl From<std::io::Error> for Error {
89 fn from(value: std::io::Error) -> Self {
90 Self::Io(value)
91 }
92}
93
94pub type Result<T> = std::result::Result<T, Error>;