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