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::{
6    coding::{DecodeError, EncodeError},
7    version::Version,
8    Checksum, CompressionType,
9};
10
11/// Represents errors that can occur in the LSM-tree
12#[derive(Debug)]
13#[non_exhaustive]
14pub enum Error {
15    /// I/O error
16    Io(std::io::Error),
17
18    /// Serialization failed
19    Encode(EncodeError),
20
21    /// Deserialization failed
22    Decode(DecodeError),
23
24    /// Decompression failed
25    Decompress(CompressionType),
26
27    /// Invalid or unparsable data format version
28    InvalidVersion(Version),
29
30    /// Some required segments could not be required from disk
31    Unrecoverable,
32
33    /// Invalid checksum value (got, expected)
34    InvalidChecksum((Checksum, Checksum)),
35
36    /// Value log errors
37    ValueLog(value_log::Error),
38}
39
40impl std::fmt::Display for Error {
41    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42        write!(f, "LsmTreeError: {self:?}")
43    }
44}
45
46impl std::error::Error for Error {}
47
48impl From<std::io::Error> for Error {
49    fn from(value: std::io::Error) -> Self {
50        Self::Io(value)
51    }
52}
53
54impl From<EncodeError> for Error {
55    fn from(value: EncodeError) -> Self {
56        Self::Encode(value)
57    }
58}
59
60impl From<DecodeError> for Error {
61    fn from(value: DecodeError) -> Self {
62        Self::Decode(value)
63    }
64}
65
66impl From<value_log::Error> for Error {
67    fn from(value: value_log::Error) -> Self {
68        Self::ValueLog(value)
69    }
70}
71
72/// Tree result
73pub type Result<T> = std::result::Result<T, Error>;