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
68
69
70
71
72
73
74
75
76
77
78
79
use std::{error::Error as StdError, fmt, result};

use serde::{de, ser};

#[derive(Debug)]
pub enum Error {
	/// This type of serialization or deserialization is not supported
	Unsupported(String),
	/// The value is too large, e.g. trying to serialize `u64` that is too large to fit in `i64`
	ValueTooLarge(String),
	/// General error during serialization
	Serialization(String),
	/// General error during deserialization
	Deserialization { column: Option<String>, message: String },
	/// Error originating from rusqlite
	Rusqlite(rusqlite::Error),
	/// No column name information available
	ColumnNamesNotAvailable,
}

pub type Result<T> = result::Result<T, Error>;

impl Error {
	/// Create the instance of `Unsupported` during serialization `Error`
	pub fn ser_unsupported(typ: &str) -> Self {
		Error::Unsupported(format!("Serialization is not supported from type: {}", typ))
	}

	/// Create the instance of `Unsupported` during deserialization `Error`
	pub fn de_unsupported(typ: &str) -> Self {
		Error::Unsupported(format!("Deserialization is not supported into type: {}", typ))
	}
}

impl fmt::Display for Error {
	fn fmt(&self, f: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
		match self {
			Error::Unsupported(s) | Error::ValueTooLarge(s) => write!(f, "{}", s),
			Error::Serialization(s) => write!(f, "Serialization error: {}", s),
			Error::Deserialization { column: None, message } => write!(f, "Deserialization error: {}", message),
			Error::Deserialization {
				column: Some(column),
				message,
			} => write!(f, "Deserialization failed for column: {} error: {}", column, message),
			Error::Rusqlite(s) => write!(f, "Rusqlite error: {}", s),
			Error::ColumnNamesNotAvailable => write!(f, "Column names are not available"),
		}
	}
}

impl StdError for Error {
	fn source(&self) -> Option<&(dyn StdError + 'static)> {
		match self {
			Error::Rusqlite(e) => Some(e),
			_ => None,
		}
	}
}

impl de::Error for Error {
	fn custom<T: fmt::Display>(msg: T) -> Self {
		Error::Deserialization {
			column: None,
			message: msg.to_string(),
		}
	}
}

impl ser::Error for Error {
	fn custom<T: fmt::Display>(msg: T) -> Self {
		Error::Serialization(msg.to_string())
	}
}

impl From<rusqlite::Error> for Error {
	fn from(e: rusqlite::Error) -> Self {
		Error::Rusqlite(e)
	}
}