1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use crate::{client_type_error, RedisGraphResult, ResultSet};
pub trait FromTable: Sized {
fn from_table(result_set: &ResultSet) -> RedisGraphResult<Self>;
}
pub trait FromRow: Sized {
fn from_row(result_set: &ResultSet, row_idx: usize) -> RedisGraphResult<Self>;
}
pub trait FromCell: Sized {
fn from_cell(
result_set: &ResultSet,
row_idx: usize,
column_idx: usize,
) -> RedisGraphResult<Self>;
}
impl FromTable for ResultSet {
fn from_table(result_set: &ResultSet) -> RedisGraphResult<Self> {
Ok(result_set.clone())
}
}
impl<T: FromRow> FromTable for Vec<T> {
fn from_table(result_set: &ResultSet) -> RedisGraphResult<Self> {
let num_rows = result_set.num_rows();
let mut ret = Self::with_capacity(num_rows);
for i in 0..num_rows {
ret.push(T::from_row(result_set, i)?);
}
Ok(ret)
}
}
macro_rules! impl_row_for_tuple {
() => ();
($($name:ident,)+) => (
#[doc(hidden)]
impl<$($name: FromCell),*> FromRow for ($($name,)*) {
#[allow(non_snake_case, unused_variables, clippy::eval_order_dependence)]
fn from_row(result_set: &ResultSet, row_idx: usize) -> RedisGraphResult<($($name,)*)> {
let mut n = 0;
$(let $name = (); n += 1;)*
if result_set.num_columns() != n {
return client_type_error!(
"failed to construct tuple: tuple has {:?} entries but result table has {:?} columns",
n,
result_set.num_columns()
);
}
let mut i = 0;
Ok(($({let $name = (); $name::from_cell(result_set, row_idx, { i += 1; i - 1 })?},)*))
}
}
impl_row_for_tuple_peel!($($name,)*);
)
}
macro_rules! impl_row_for_tuple_peel {
($name:ident, $($other:ident,)*) => (impl_row_for_tuple!($($other,)*);)
}
impl_row_for_tuple! { T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, }
impl<T: FromCell> FromRow for T {
fn from_row(result_set: &ResultSet, row_idx: usize) -> RedisGraphResult<Self> {
T::from_cell(result_set, row_idx, 0)
}
}
impl<T: FromRow> FromTable for T {
fn from_table(result_set: &ResultSet) -> RedisGraphResult<Self> {
T::from_row(result_set, 0)
}
}