use super::{Row, SqlError};
pub trait TryFromRow: Sized {
fn from_row(row: &Row) -> Result<Self, SqlError>;
}
impl TryFromRow for () {
fn from_row(_row: &Row) -> Result<Self, SqlError> {
Ok(())
}
}
macro_rules! try_from_row {
($impl_type:ty) => {
impl TryFromRow for $impl_type {
fn from_row(row: &Row) -> Result<Self, SqlError> {
row.try_get(0)
}
}
impl TryFromRow for Option<$impl_type> {
fn from_row(row: &Row) -> Result<Self, SqlError> {
row.try_get(0)
}
}
impl TryFromRow for Vec<$impl_type> {
fn from_row(row: &Row) -> Result<Self, SqlError> {
row.try_get(0)
}
}
impl TryFromRow for Vec<Option<$impl_type>> {
fn from_row(row: &Row) -> Result<Self, SqlError> {
row.try_get(0)
}
}
impl TryFromRow for Option<Vec<$impl_type>> {
fn from_row(row: &Row) -> Result<Self, SqlError> {
row.try_get(0)
}
}
impl TryFromRow for Option<Vec<Option<$impl_type>>> {
fn from_row(row: &Row) -> Result<Self, SqlError> {
row.try_get(0)
}
}
};
}
try_from_row!(bool);
try_from_row!(i8);
try_from_row!(i16);
try_from_row!(i32);
try_from_row!(u32);
try_from_row!(i64);
try_from_row!(f32);
try_from_row!(f64);
try_from_row!(String);
try_from_row!(Vec<u8>);
try_from_row!(std::collections::HashMap<String, Option<String>>);
try_from_row!(std::time::SystemTime);
try_from_row!(std::net::IpAddr);
use postgres_types::FromSqlOwned;
macro_rules! try_from_tuple {
($($typ_name:ident),*, $($number:literal),*) => {
impl< $($typ_name:FromSqlOwned),* > TryFromRow for ($($typ_name),*,) {
fn from_row(row: &Row) -> Result<Self, SqlError> {
Ok((
$(row.try_get($number)?),*,
))
}
}
};
}
try_from_tuple!(A, 1);
try_from_tuple!(A, B, 1, 2);
try_from_tuple!(A, B, C, 1, 2, 3);
try_from_tuple!(A, B, C, D, 1, 2, 3, 4);
try_from_tuple!(A, B, C, D, E, 1, 2, 3, 4, 5);
try_from_tuple!(A, B, C, D, E, F, 1, 2, 3, 4, 5, 6);
try_from_tuple!(A, B, C, D, E, F, G, 1, 2, 3, 4, 5, 6, 7);
try_from_tuple!(A, B, C, D, E, F, G, H, 1, 2, 3, 4, 5, 6, 7, 8);
try_from_tuple!(A, B, C, D, E, F, G, H, I, 1, 2, 3, 4, 5, 6, 7, 8, 9);
try_from_tuple!(A, B, C, D, E, F, G, H, I, J, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
try_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11);
try_from_tuple!(A, B, C, D, E, F, G, H, I, J, K, L, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
#[cfg(feature = "with-bit-vec-0_6")]
try_from_row!(bit_vec::BitVec);
#[cfg(feature = "with-chrono-0_4")]
mod chrono_impls {
use super::*;
try_from_row!(chrono::NaiveDateTime);
try_from_row!(chrono::DateTime<chrono::Utc>);
try_from_row!(chrono::DateTime<chrono::Local>);
try_from_row!(chrono::DateTime<chrono::FixedOffset>);
try_from_row!(chrono::NaiveDate);
try_from_row!(chrono::NaiveTime);
}
#[cfg(feature = "with-eui48-0_4")]
try_from_row!(eui48::MacAddress);
#[cfg(feature = "geo-types")]
mod geo_types_impls {
use super::*;
try_from_row!(geo_types::Point<f64>);
try_from_row!(geo_types::Rect<f64>);
try_from_row!(geo_types::LineString<f64>);
}
#[cfg(feature = "rust_decimal")]
try_from_row!(rust_decimal::Decimal);
#[cfg(feature = "serde_json")]
try_from_row!(serde_json::Value);
#[cfg(feature = "with-time-0_2")]
mod time_impls {
use super::*;
try_from_row!(time::PrimitiveDateTime);
try_from_row!(time::OffsetDateTime);
try_from_row!(time::Date);
try_from_row!(time::Time);
}
#[cfg(feature = "uuid")]
try_from_row!(uuid::Uuid);