123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171 |
- //! Collection of macros to generate code to digest data from SQLite
- /// Unpacks a vector of Column, and consumes it, parsing into individual variables, checking the
- /// vector is big enough.
- #[macro_export]
- macro_rules! unpack_into {
- (let ($($var:ident),+) = $array:expr) => {
- let ($($var),+) = {
- let mut vec = $array.to_vec();
- vec.reverse();
- let required = 0 $(+ {let _ = stringify!($var); 1})+;
- if vec.len() < required {
- return Err(Error::MissingColumn(required, vec.len()));
- }
- Ok::<_, Error>((
- $(
- vec.pop().expect(&format!("Checked length already for {}", stringify!($var)))
- ),+
- ))?
- };
- };
- }
- /// Parses a SQLite column as a string or NULL
- #[macro_export]
- macro_rules! column_as_nullable_string {
- ($col:expr, $callback_str:expr, $callback_bytes:expr) => {
- (match $col {
- $crate::stmt::Column::Text(text) => Ok(Some(text).and_then($callback_str)),
- $crate::stmt::Column::Blob(bytes) => Ok(Some(bytes).and_then($callback_bytes)),
- $crate::stmt::Column::Null => Ok(None),
- other => Err(Error::InvalidType(
- "String".to_owned(),
- other.data_type().to_string(),
- )),
- })?
- };
- ($col:expr, $callback_str:expr) => {
- (match $col {
- $crate::stmt::Column::Text(text) => Ok(Some(text).and_then($callback_str)),
- $crate::stmt::Column::Blob(bytes) => {
- Ok(Some(String::from_utf8_lossy(&bytes)).and_then($callback_str))
- }
- $crate::stmt::Column::Null => Ok(None),
- other => Err(Error::InvalidType(
- "String".to_owned(),
- other.data_type().to_string(),
- )),
- })?
- };
- ($col:expr) => {
- (match $col {
- $crate::stmt::Column::Text(text) => Ok(Some(text.to_owned())),
- $crate::stmt::Column::Blob(bytes) => {
- Ok(Some(String::from_utf8_lossy(&bytes).to_string()))
- }
- $crate::stmt::Column::Null => Ok(None),
- other => Err(Error::InvalidType(
- "String".to_owned(),
- other.data_type().to_string(),
- )),
- })?
- };
- }
- /// Parses a column as a number or NULL
- #[macro_export]
- macro_rules! column_as_nullable_number {
- ($col:expr) => {
- (match $col {
- $crate::stmt::Column::Text(text) => Ok(Some(text.parse().map_err(|_| {
- Error::InvalidConversion(stringify!($col).to_owned(), "Number".to_owned())
- })?)),
- $crate::stmt::Column::Integer(n) => Ok(Some(n.try_into().map_err(|_| {
- Error::InvalidConversion(stringify!($col).to_owned(), "Number".to_owned())
- })?)),
- $crate::stmt::Column::Null => Ok(None),
- other => Err(Error::InvalidType(
- "Number".to_owned(),
- other.data_type().to_string(),
- )),
- })?
- };
- }
- /// Parses a column as a number
- #[macro_export]
- macro_rules! column_as_number {
- ($col:expr) => {
- (match $col {
- $crate::stmt::Column::Text(text) => text.parse().map_err(|_| {
- Error::InvalidConversion(stringify!($col).to_owned(), "Number".to_owned())
- }),
- $crate::stmt::Column::Integer(n) => n.try_into().map_err(|_| {
- Error::InvalidConversion(stringify!($col).to_owned(), "Number".to_owned())
- }),
- other => Err(Error::InvalidType(
- "Number".to_owned(),
- other.data_type().to_string(),
- )),
- })?
- };
- }
- /// Parses a column as a NULL or Binary
- #[macro_export]
- macro_rules! column_as_nullable_binary {
- ($col:expr) => {
- (match $col {
- $crate::stmt::Column::Text(text) => Ok(Some(text.as_bytes().to_vec())),
- $crate::stmt::Column::Blob(bytes) => Ok(Some(bytes.to_owned())),
- $crate::stmt::Column::Null => Ok(None),
- other => Err(Error::InvalidType(
- "String".to_owned(),
- other.data_type().to_string(),
- )),
- })?
- };
- }
- /// Parses a SQLite column as a binary
- #[macro_export]
- macro_rules! column_as_binary {
- ($col:expr) => {
- (match $col {
- $crate::stmt::Column::Text(text) => Ok(text.as_bytes().to_vec()),
- $crate::stmt::Column::Blob(bytes) => Ok(bytes.to_owned()),
- other => Err(Error::InvalidType(
- "String".to_owned(),
- other.data_type().to_string(),
- )),
- })?
- };
- }
- /// Parses a SQLite column as a string
- #[macro_export]
- macro_rules! column_as_string {
- ($col:expr, $callback_str:expr, $callback_bytes:expr) => {
- (match $col {
- $crate::stmt::Column::Text(text) => $callback_str(&text).map_err(Error::from),
- $crate::stmt::Column::Blob(bytes) => $callback_bytes(&bytes).map_err(Error::from),
- other => Err(Error::InvalidType(
- "String".to_owned(),
- other.data_type().to_string(),
- )),
- })?
- };
- ($col:expr, $callback:expr) => {
- (match $col {
- $crate::stmt::Column::Text(text) => $callback(&text).map_err(Error::from),
- $crate::stmt::Column::Blob(bytes) => {
- $callback(&String::from_utf8_lossy(&bytes)).map_err(Error::from)
- }
- other => Err(Error::InvalidType(
- "String".to_owned(),
- other.data_type().to_string(),
- )),
- })?
- };
- ($col:expr) => {
- (match $col {
- $crate::stmt::Column::Text(text) => Ok(text.to_owned()),
- $crate::stmt::Column::Blob(bytes) => Ok(String::from_utf8_lossy(&bytes).to_string()),
- other => Err(Error::InvalidType(
- "String".to_owned(),
- other.data_type().to_string(),
- )),
- })?
- };
- }
|