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
45impl std::fmt::Display for Error {
46 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
47 write!(f, "LsmTreeError: {self:?}")
48 }
49}
50
51impl std::error::Error for Error {
52 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
53 match self {
54 Self::Io(e) => Some(e),
55 _ => None,
56 }
57 }
58}
59
60impl From<sfa::Error> for Error {
61 fn from(value: sfa::Error) -> Self {
62 match value {
63 sfa::Error::Io(e) => Self::from(e),
64 sfa::Error::ChecksumMismatch { got, expected } => {
65 log::error!("Archive ToC checksum mismatch");
66 Self::ChecksumMismatch {
67 got: got.into(),
68 expected: expected.into(),
69 }
70 }
71 sfa::Error::InvalidHeader => {
72 log::error!("Invalid archive header");
73 Self::Unrecoverable
74 }
75 sfa::Error::InvalidVersion => {
76 log::error!("Invalid archive version");
77 Self::Unrecoverable
78 }
79 sfa::Error::UnsupportedChecksumType => {
80 log::error!("Invalid archive checksum type");
81 Self::Unrecoverable
82 }
83 }
84 }
85}
86
87impl From<std::io::Error> for Error {
88 fn from(value: std::io::Error) -> Self {
89 Self::Io(value)
90 }
91}
92
93pub type Result<T> = std::result::Result<T, Error>;