rusqlite_from_row_fork2/
lib.rs

1#![deny(missing_docs)]
2#![doc = include_str!("../README.md")]
3
4pub use rusqlite;
5pub use rusqlite_from_row_derive::FromRow;
6
7/// A trait that allows mapping a [`rusqlite::Row`] to other types.
8pub trait FromRow: Sized {
9    /// Performs the conversion.
10    ///
11    /// # Panics
12    ///
13    /// Panics if the row does not contain the expected column names.
14    fn from_row(row: &rusqlite::Row) -> Self {
15        Self::from_row_prefixed(row, None)
16    }
17
18    /// Try's to perform the conversion.
19    ///
20    /// Will return an error if the row does not contain the expected column names.
21    fn try_from_row(row: &rusqlite::Row) -> Result<Self, rusqlite::Error> {
22        Self::try_from_row_prefixed(row, None)
23    }
24
25    /// Perform the conversion. Each row will be extracted using it's name prefixed with
26    /// `prefix`.
27    ///
28    /// # Panics
29    ///
30    /// Panics if the row does not contain the expected column names.
31    fn from_row_prefixed(row: &rusqlite::Row, prefix: Option<&str>) -> Self {
32        Self::try_from_row_prefixed(row, prefix).expect("from row failed")
33    }
34
35    /// Try's to perform the conversion. Each row will be extracted using it's name prefixed with
36    /// `prefix`.
37    ///
38    /// Will return an error if the row does not contain the expected column names.
39    fn try_from_row_prefixed(
40        row: &rusqlite::Row,
41        prefix: Option<&str>,
42    ) -> Result<Self, rusqlite::Error>;
43
44    /// Try's to check if all the columns that are needed by this struct are sql 'null' values.
45    ///
46    /// Will return an error if the row does not contain the expected column names.
47    fn is_all_null(row: &rusqlite::Row, prefix: Option<&str>) -> Result<bool, rusqlite::Error>;
48}
49
50impl<T: FromRow> FromRow for Option<T> {
51    fn try_from_row_prefixed(
52        row: &rusqlite::Row,
53        prefix: Option<&str>,
54    ) -> Result<Self, rusqlite::Error> {
55        if T::is_all_null(row, prefix)? {
56            Ok(None)
57        } else {
58            Ok(Some(T::try_from_row_prefixed(row, prefix)?))
59        }
60    }
61
62    fn is_all_null(row: &rusqlite::Row, prefix: Option<&str>) -> Result<bool, rusqlite::Error> {
63        T::is_all_null(row, prefix)
64    }
65}