odbc_safe/
return_.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 Return<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    /// An error occured.
12    Error(E),
13}
14pub use Return::{Error, Info, Success};
15
16impl<T, E> Return<T, E> {
17    /// Maps a `Return<T,E>` to `Return<U,E>` by applying a function to a contained `Success` or
18    /// `Info` value, leaving an `Error` value untouched.
19    pub fn map<F, U>(self, f: F) -> Return<U, E>
20    where
21        F: FnOnce(T) -> U,
22    {
23        match self {
24            Success(v) => Success(f(v)),
25            Info(v) => Info(f(v)),
26            Error(e) => Error(e),
27        }
28    }
29
30    /// Maps a `Return<T,E>` to `Result<T,U>` by applying a function to a contained `Error value,
31    /// leaving a `Success` or an `Info` value untouched.
32    pub fn map_error<F, U>(self, f: F) -> Return<T, U>
33    where
34        F: FnOnce(E) -> U,
35    {
36        match self {
37            Success(v) => Success(v),
38            Info(v) => Info(v),
39            Error(e) => Error(f(e)),
40        }
41    }
42
43    /// Unwraps the result, yielding the content of `Success` or `Info`
44    pub fn unwrap(self) -> T {
45        match self {
46            Success(v) | Info(v) => v,
47            Error(_) => {
48                panic!("Unwrapping `Return` failed. Use diagnostics to obtain more information.")
49            }
50        }
51    }
52
53    /// Transforms the `Return<T,E>` into a `Result<T,U>`, mapping `Success(v) | Info(v)` to
54    /// `Ok(v)` and `Error(err)` to `Err(err.into())`.
55    pub fn success<U>(self) -> Result<T, U>
56    where
57        U: From<E>,
58    {
59        match self {
60            Success(v) | Info(v) => Ok(v),
61            Error(e) => Err(e.into()),
62        }
63    }
64}
65
66impl From<SQLRETURN> for Return<()> {
67    fn from(source: SQLRETURN) -> Return<()> {
68        match source {
69            SQL_SUCCESS => Success(()),
70            SQL_SUCCESS_WITH_INFO => Info(()),
71            SQL_ERROR => Error(()),
72            other => panic!("Unexpected SQLRETURN value: {:?}", other),
73        }
74    }
75}