Skip to main content

sea_schema/sqlite/
error.rs

1use std::num::{ParseFloatError, ParseIntError};
2
3use crate::rusqlite_types::RusqliteError;
4
5/// This type simplifies error handling
6pub type DiscoveryResult<T> = Result<T, SqliteDiscoveryError>;
7
8/// All the errors that can be encountered when using this module
9#[derive(Debug)]
10pub enum SqliteDiscoveryError {
11    /// An error parsing a string from the result of an SQLite query into an rust-language integer
12    ParseIntError,
13    /// An error parsing a string from the result of an SQLite query into an rust-language float
14    ParseFloatError,
15    /// The target index was not found
16    IndexNotFound(String),
17    /// The error as defined in [RusqliteError]
18    RusqliteError(RusqliteError),
19}
20
21impl From<ParseIntError> for SqliteDiscoveryError {
22    fn from(_: ParseIntError) -> Self {
23        SqliteDiscoveryError::ParseIntError
24    }
25}
26
27impl From<ParseFloatError> for SqliteDiscoveryError {
28    fn from(_: ParseFloatError) -> Self {
29        SqliteDiscoveryError::ParseFloatError
30    }
31}
32
33impl From<RusqliteError> for SqliteDiscoveryError {
34    fn from(error: RusqliteError) -> Self {
35        SqliteDiscoveryError::RusqliteError(error)
36    }
37}
38
39impl std::error::Error for SqliteDiscoveryError {}
40
41impl std::fmt::Display for SqliteDiscoveryError {
42    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
43        match self {
44            SqliteDiscoveryError::ParseIntError => write!(f, "Parse Integer Error"),
45            SqliteDiscoveryError::ParseFloatError => write!(f, "Parse Float Error"),
46            SqliteDiscoveryError::IndexNotFound(index) => write!(f, "Index Not Found: {index}"),
47            SqliteDiscoveryError::RusqliteError(e) => write!(f, "Rusqlite Error: {e:?}"),
48        }
49    }
50}