rapiddb/errors/
mod.rs

1/// An error in this library.
2///
3/// ## Examples
4/// ```no_run
5/// let check_size = |size: usize| -> Result<(), rapiddb::errors::Error> {
6///     if size > 4000000 {
7///         return Err(rapiddb::errors::Error::FileFull);
8///     }
9///     if size > 9999 {
10///         return Err(rapiddb::errors::Error::ArrayFull);
11///     }
12///
13///     Ok(())
14/// };
15///
16/// check_size(10000).unwrap_or_else(|error| match error {
17///     rapiddb::errors::Error::FileFull => {
18///         println!("handle FileFull here");
19///     }
20///     rapiddb::errors::Error::ArrayFull => {
21///         println!("handle ArrayFull here");
22///     }
23///     _ => (),
24/// });
25/// ```
26#[derive(Debug)]
27pub enum Error {
28  SizeCorrupted,
29  SeekCorrupted,
30  FileFull,
31  ArrayFull,
32  ArrayEmpty,
33  IndexOutOfRange,
34  IndexOutOfBounds,
35  StdNumParseIntError(std::num::ParseIntError),
36  StdIoError(std::io::Error),
37  StdArrayTryFromSliceError(std::array::TryFromSliceError),
38}
39
40impl From<std::num::ParseIntError> for Error {
41  fn from(error: std::num::ParseIntError) -> Self {
42    Self::StdNumParseIntError(error)
43  }
44}
45
46impl From<std::io::Error> for Error {
47  fn from(error: std::io::Error) -> Self {
48    Self::StdIoError(error)
49  }
50}
51
52impl From<std::array::TryFromSliceError> for Error {
53  fn from(error: std::array::TryFromSliceError) -> Self {
54    Self::StdArrayTryFromSliceError(error)
55  }
56}
57
58impl std::fmt::Display for Error {
59  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
60    match self {
61      Self::SizeCorrupted => write!(f, "Size corrupted"),
62      Self::SeekCorrupted => write!(f, "Seek corrupted"),
63      Self::FileFull => write!(f, "File is full"),
64      Self::ArrayFull => write!(f, "Array is full"),
65      Self::ArrayEmpty => write!(f, "Array is empty"),
66      Self::IndexOutOfRange => write!(f, "Index out of range"),
67      Self::IndexOutOfBounds => write!(f, "Index out of bounds"),
68      Self::StdNumParseIntError(e) => std::fmt::Display::fmt(e, f),
69      Self::StdIoError(e) => std::fmt::Display::fmt(e, f),
70      Self::StdArrayTryFromSliceError(e) => std::fmt::Display::fmt(e, f),
71    }
72  }
73}
74
75impl std::error::Error for Error {}