From 36e18e6b28387d9aeb6b27f85bae1bfd6a0d8b05 Mon Sep 17 00:00:00 2001 From: Edgar Date: Mon, 4 Mar 2024 13:48:42 -0800 Subject: [PATCH] Add support for deserializing Vec> diff comment --- src/types/from_sql.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/types/from_sql.rs b/src/types/from_sql.rs index a5e580d..a327482 100644 --- a/src/types/from_sql.rs +++ b/src/types/from_sql.rs @@ -176,7 +176,10 @@ impl<'a> FromSql<'a> for uuid::Uuid { } macro_rules! from_sql_vec_impl { - ( $( $t:ty: $k:pat => $f:expr ),* ) => { + // Base case: Vec + ( + $($t:ty: $k:pat => $f:expr),* + ) => { $( impl<'a> FromSql<'a> for Vec<$t> { fn from_sql(value: ValueRef<'a>) -> FromSqlResult { @@ -200,6 +203,36 @@ macro_rules! from_sql_vec_impl { } } } + // Recursive case: Vec> + impl<'a> FromSql<'a> for Vec> { + fn from_sql(value: ValueRef<'a>) -> FromSqlResult { + match value { + ValueRef::Array(SqlType::Array(_), outer_vs) => { + let mut result = Vec::with_capacity(outer_vs.len()); + for outer_v in outer_vs.iter() { + match outer_v { + ValueRef::Array($k, inner_vs) => { + let f: fn(ValueRef<'a>) -> FromSqlResult<$t> = $f; + let inner: Vec<$t> = inner_vs.iter().map(|inner_v| f(inner_v.clone())).collect::, _>>()?; + result.push(inner); + } + _ => { + return Err(Error::FromSql(FromSqlError::InvalidType { + src: SqlType::from(outer_v.clone()).to_string(), + dst: format!("Vec>", stringify!($t)).into(), + })); + } + } + } + Ok(result) + } + _ => Err(Error::FromSql(FromSqlError::InvalidType { + src: SqlType::from(value.clone()).to_string(), + dst: format!("Vec>", stringify!($t)).into(), + })), + } + } + } )* }; }