lsm_tree/
error.rs

1// Copyright (c) 2024-present, fjall-rs
2// This source code is licensed under both the Apache 2.0 and MIT License
3// (found in the LICENSE-* files in the repository)
4
5use crate::{Checksum, CompressionType};
6
7/// Represents errors that can occur in the LSM-tree
8#[derive(Debug)]
9#[non_exhaustive]
10pub enum Error {
11    /// I/O error
12    Io(std::io::Error),
13
14    /// Decompression failed
15    Decompress(CompressionType),
16
17    /// Invalid or unparsable data format version
18    InvalidVersion(u8),
19
20    /// Some required files could not be recovered from disk
21    Unrecoverable,
22
23    /// Checksum mismatch
24    ChecksumMismatch {
25        /// Checksum of loaded block
26        got: Checksum,
27
28        /// Checksum that was saved in block header
29        expected: Checksum,
30    },
31
32    /// Invalid enum tag
33    InvalidTag((&'static str, u8)),
34
35    /// Invalid block trailer
36    InvalidTrailer,
37
38    /// Invalid block header
39    InvalidHeader(&'static str),
40
41    /// UTF-8 error
42    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
93/// Tree result
94pub type Result<T> = std::result::Result<T, Error>;