odbc_safe/
return_option.rs

1use sys::*;
2
3/// Holds result and indicates the overall success or failure of a function.
4#[derive(Debug)]
5#[must_use]
6pub enum ReturnOption<T, E = ()> {
7    /// The function has been executed successfully. Holds result.
8    Success(T),
9    /// The function has been executed successfully. There have been warnings. Holds result.
10    Info(T),
11    /// No more data was available
12    NoData(E),
13    /// An error occured.
14    Error(E),
15}
16
17impl<T, E> ReturnOption<T, E> {
18    /// Maps a `ReturnOption<T,E>` to `ReturnOption<U,E>` by applying a function to a contained
19    /// `Success` or `Info` value, leaving an `Error` or `NoData` value untouched.
20    pub fn map<F, U>(self, f: F) -> ReturnOption<U, E>
21    where
22        F: FnOnce(T) -> U,
23    {
24        match self {
25            ReturnOption::Success(t) => ReturnOption::Success(f(t)),
26            ReturnOption::Info(t) => ReturnOption::Info(f(t)),
27            ReturnOption::NoData(e) => ReturnOption::NoData(e),
28            ReturnOption::Error(e) => ReturnOption::Error(e),
29        }
30    }
31}
32
33impl From<SQLRETURN> for ReturnOption<()> {
34    fn from(source: SQLRETURN) -> ReturnOption<()> {
35        match source {
36            SQL_SUCCESS => ReturnOption::Success(()),
37            SQL_SUCCESS_WITH_INFO => ReturnOption::Info(()),
38            SQL_ERROR => ReturnOption::Error(()),
39            SQL_NO_DATA => ReturnOption::NoData(()),
40            other => panic!("Unexpected SQLRETURN value: {:?}", other),
41        }
42    }
43}