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 recovered 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    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
48        match self {
49            Self::Io(e) => Some(e),
50            Self::Encode(e) => Some(e),
51            Self::Decode(e) => Some(e),
52            Self::ValueLog(e) => Some(e),
53            Self::Decompress(_)
54            | Self::InvalidVersion(_)
55            | Self::Unrecoverable
56            | Self::InvalidChecksum(_) => None,
57        }
58    }
59}
60
61impl From<std::io::Error> for Error {
62    fn from(value: std::io::Error) -> Self {
63        Self::Io(value)
64    }
65}
66
67impl From<EncodeError> for Error {
68    fn from(value: EncodeError) -> Self {
69        Self::Encode(value)
70    }
71}
72
73impl From<DecodeError> for Error {
74    fn from(value: DecodeError) -> Self {
75        Self::Decode(value)
76    }
77}
78
79impl From<value_log::Error> for Error {
80    fn from(value: value_log::Error) -> Self {
81        Self::ValueLog(value)
82    }
83}
84
85/// Tree result
86pub type Result<T> = std::result::Result<T, Error>;