1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use core::array::TryFromSliceError;
use core::fmt::Display;
#[cfg(feature = "std")]
use std::error::Error as StdError;
#[cfg(feature = "std")]
use std::io::Error as StdioError;

pub type SQLiteResult<T> = Result<T, SQLiteError>;

#[derive(Debug)]
pub enum SQLiteError {
  HeaderValidationError(&'static str),
  TryFromSliceError(TryFromSliceError),
  #[cfg(feature = "std")]
  StdioError(StdioError),
  Custom(&'static str),
  ParsingField(FieldParsingError),
  InvalidPayloadSize(InvalidPayloadSizeError),
}

#[derive(Debug)]
pub struct FieldParsingError {
  pub error: &'static str,
  pub ty: &'static str,
}

#[derive(Debug)]
pub struct InvalidPayloadSizeError {
  pub error: &'static str,
  pub ty: &'static str,
}

// impl SQLiteError {
//   pub fn msg(msg: &str) -> Self {
//     Self::Custom(msg.into())
//   }
// }

impl Display for SQLiteError {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    // TODO
    write!(f, "{:?}", self)
  }
}

impl From<TryFromSliceError> for SQLiteError {
  fn from(error: TryFromSliceError) -> Self {
    Self::TryFromSliceError(error)
  }
}

// #[cfg(feature = "alloc")]
// impl From<String> for SQLiteError {
//   fn from(s: String) -> Self {
//     Self::Custom(s)
//   }
// }

#[cfg(feature = "std")]
impl StdError for SQLiteError {}

#[cfg(feature = "std")]
impl From<StdioError> for SQLiteError {
  fn from(io_error: StdioError) -> Self {
    Self::StdioError(io_error)
  }
}