serde_odbc/
error.rs

1/*
2This file is part of serde-odbc.
3
4serde-odbc is free software: you can redistribute it and/or modify
5it under the terms of the GNU Lesser General Public License as published by
6the Free Software Foundation, either version 3 of the License, or
7(at your option) any later version.
8
9serde-odbc is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12GNU Lesser General Public License for more details.
13
14You should have received a copy of the GNU Lesser General Public License
15along with serde-odbc.  If not, see <http://www.gnu.org/licenses/>.
16*/
17use 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}