lazy_db/
error.rs

1use std::{fmt, error::Error, path::PathBuf};
2use crate::LazyType;
3
4#[derive(Debug)]
5pub enum LDBError {
6    IOError(std::io::Error),
7    FileNotFound(PathBuf),
8    DirNotFound(PathBuf),
9    InvalidLazyType(u8),
10    IncorrectType(LazyType, String),
11    InvalidUTF8String(Box<[u8]>),
12    InvalidNumberByteLength(u8, String),
13    InvalidMetaVersion(PathBuf),
14    IncompatibleVersion(crate::version::Version),
15}
16
17impl fmt::Display for LDBError {
18    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
19        use LDBError::*;
20        match self {
21            FileNotFound(p) => write!(f, "File '{}' not found", p.to_string_lossy()),
22            DirNotFound(p) => write!(f, "Directory '{}' not found", p.to_string_lossy()),
23            IOError(e) => write!(f, "IO Error: {:?}", e),
24            InvalidLazyType(t) => write!(f, "Invalid Lazy Type {t}"),
25            IncorrectType(t1, t2) => write!(f, "Cannot read type '{0:?}' as '{1:?}'", t1, t2),
26            InvalidUTF8String(x) => write!(f, "Bytes represent an invalid utf8 string: {:?}", x),
27            InvalidNumberByteLength(x, t) => write!(f, "Invalid byte length '{x}' for number type '{t:?}'"),
28            InvalidMetaVersion(p) => write!(f, "Invalid version for `lazy-db` at '{}'", p.to_string_lossy()),
29            IncompatibleVersion(v) => write!(f, "Found version '{v}' incompatible with current version '{}'", crate::VERSION),
30        }
31    }
32}
33
34impl Error for LDBError {}