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
//! Result types to enabling handling of ODBC Errors
use super::{DiagnosticRecord, GetDiagRec};
use std::fmt::{Display, Formatter};
use std;

/// Result type returned by most functions in this crate
pub type Result<T> = std::result::Result<T, DiagnosticRecord>;

#[must_use]
pub enum Return<T> {
    Success(T),
    SuccessWithInfo(T),
    Error,
}

impl<T> Return<T> {
    pub fn into_result<O: GetDiagRec>(self, odbc_object: &O) -> Result<T> {
        match self {
            Return::Success(value) => Ok(value),
            Return::SuccessWithInfo(value) => {
                let mut i = 1;
                while let Some(diag) = odbc_object.get_diag_rec(i) {
                    warn!("{}", diag);
                    i += 1;
                }
                Ok(value)
            }
            Return::Error => {
                // Return the first record
                let diag = odbc_object.get_diag_rec(1).unwrap();
                error!("{}", diag);
                let mut i = 2;
                // log the rest
                while let Some(diag) = odbc_object.get_diag_rec(i) {
                    error!("{}", diag);
                    i += 1;
                }
                Err(diag)
            }
        }
    }
}

/// Environment allocation error
///
/// Allocating an environment is the one operation in ODBC which does not yiel a diagnostic record
/// in case of an error. There is simply no Environment to ask for a diagnostic record
#[derive(Debug)]
pub struct EnvAllocError;

impl Display for EnvAllocError {
    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
        use std::error::Error;
        write!(f, "{}", self.description())
    }
}

impl std::error::Error for EnvAllocError {
    fn description(&self) -> &str {
        "Failure to allocate ODBC environment"
    }

    fn cause(&self) -> Option<&std::error::Error> {
        None
    }
}