Skip to content

Commit

Permalink
diff
Browse files Browse the repository at this point in the history
  • Loading branch information
0xEdgar committed Mar 4, 2024
1 parent 623515c commit 7c8feff
Showing 1 changed file with 77 additions and 17 deletions.
94 changes: 77 additions & 17 deletions src/types/from_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,64 @@ impl<'a> FromSql<'a> for uuid::Uuid {
}
}

// macro_rules! from_sql_vec_impl {
// ( $( $t:ty: $k:pat => $f:expr ),* ) => {
// $(
// impl<'a> FromSql<'a> for Vec<$t> {
// fn from_sql(value: ValueRef<'a>) -> FromSqlResult<Self> {
// match value {
// ValueRef::Array($k, vs) => {
// let f: fn(ValueRef<'a>) -> FromSqlResult<$t> = $f;
// let mut result = Vec::with_capacity(vs.len());
// for r in vs.iter() {
// let value: $t = f(r.clone())?;
// result.push(value);
// }
// Ok(result)
// }
// _ => {
// let from = SqlType::from(value.clone()).to_string();
// Err(Error::FromSql(FromSqlError::InvalidType {
// src: from,
// dst: format!("Vec<{}>", stringify!($t)).into(),
// }))
// }
// }
// }
// }
// )*
// // New implementation for nested Vec<Vec<T>>
// $(
// impl<'a> FromSql<'a> for Vec<Vec<$t>> {
// fn from_sql(value: ValueRef<'a>) -> FromSqlResult<Self> {
// match value {
// ValueRef::Array(SqlType::Array(_), outer_vs) => {
// outer_vs.iter().map(|outer_v| match outer_v {
// ValueRef::Array($k, inner_vs) => {
// let f: fn(ValueRef<'a>) -> FromSqlResult<$t> = $f;
// inner_vs.iter().map(|inner_v| f(inner_v.clone())).collect()
// },
// _ => Err(Error::FromSql(FromSqlError::InvalidType {
// src: SqlType::from(outer_v.clone()).to_string(),
// dst: format!("Vec<Vec<{}>>", stringify!($t)).into(),
// })),
// }).collect()
// },
// _ => Err(Error::FromSql(FromSqlError::InvalidType {
// src: SqlType::from(value.clone()).to_string(),
// dst: "Vec<Vec<_>>".into(),
// })),
// }
// }
// }
// )*

// };
// }
macro_rules! from_sql_vec_impl {
( $( $t:ty: $k:pat => $f:expr ),* ) => {
(
$($t:ty: $k:pat => $f:expr),*
) => {
$(
impl<'a> FromSql<'a> for Vec<$t> {
fn from_sql(value: ValueRef<'a>) -> FromSqlResult<Self> {
Expand All @@ -200,33 +256,37 @@ macro_rules! from_sql_vec_impl {
}
}
}
)*
// New implementation for nested Vec<Vec<T>>
$(

impl<'a> FromSql<'a> for Vec<Vec<$t>> {
fn from_sql(value: ValueRef<'a>) -> FromSqlResult<Self> {
match value {
ValueRef::Array(SqlType::Array(_), outer_vs) => {
outer_vs.iter().map(|outer_v| match outer_v {
ValueRef::Array($k, inner_vs) => {
let f: fn(ValueRef<'a>) -> FromSqlResult<$t> = $f;
inner_vs.iter().map(|inner_v| f(inner_v.clone())).collect()
},
_ => Err(Error::FromSql(FromSqlError::InvalidType {
src: SqlType::from(outer_v.clone()).to_string(),
dst: format!("Vec<Vec<{}>>", stringify!($t)).into(),
})),
}).collect()
},
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::<std::result::Result<Vec<$t>, _>>()?;
result.push(inner);
}
_ => {
return Err(Error::FromSql(FromSqlError::InvalidType {
src: SqlType::from(outer_v.clone()).to_string(),
dst: format!("Vec<Vec<{}>>", stringify!($t)).into(),
}));
}
}
}
Ok(result)
}
_ => Err(Error::FromSql(FromSqlError::InvalidType {
src: SqlType::from(value.clone()).to_string(),
dst: "Vec<Vec<_>>".into(),
dst: format!("Vec<Vec<{}>>", stringify!($t)).into(),
})),
}
}
}
)*

};
}

Expand Down

0 comments on commit 7c8feff

Please sign in to comment.