1use std::error;
18use std::fmt;
19use std::result;
20
21use odbc_sys::{SQLRETURN, SQL_NO_DATA, SQL_SUCCESS, SQL_SUCCESS_WITH_INFO};
22use serde::ser;
23
24#[derive(Debug)]
25pub enum Error {
26 Odbc(SQLRETURN),
27 Serde(String),
28}
29
30pub type Result<T> = result::Result<T, Error>;
31
32impl error::Error for Error {
33 fn description(&self) -> &str {
34 match *self {
35 Error::Odbc(_) => "ODBC error",
36 Error::Serde(_) => "Serde error",
37 }
38 }
39}
40
41impl fmt::Display for Error {
42 fn fmt(&self, fmt: &mut fmt::Formatter) -> result::Result<(), fmt::Error> {
43 match *self {
44 Error::Odbc(rc) => write!(fmt, "ODBC error: {:?}", rc),
45 Error::Serde(ref msg) => write!(fmt, "Serde error: {}", msg),
46 }
47 }
48}
49
50impl ser::Error for Error {
51 fn custom<T: fmt::Display>(msg: T) -> Self {
52 Error::Serde(msg.to_string())
53 }
54}
55
56pub trait OdbcResult {
57 fn check(self) -> Result<()>;
58}
59
60impl OdbcResult for SQLRETURN {
61 fn check(self) -> Result<()> {
62 match self {
63 SQL_SUCCESS | SQL_SUCCESS_WITH_INFO | SQL_NO_DATA => Ok(()),
64 rc => Err(Error::Odbc(rc)),
65 }
66 }
67}