wasccgraph_common/
conversions.rs

1use crate::{
2    client_type_error, FromCell, GraphError, GraphResult, GraphString, Node, Relation, ResultSet,
3    Scalar,
4};
5
6impl FromCell for Scalar {
7    fn from_cell(result_set: &ResultSet, row_idx: usize, column_idx: usize) -> GraphResult<Self> {
8        let scalar = result_set.get_scalar(row_idx, column_idx)?;
9        Ok(scalar.clone())
10    }
11}
12
13impl FromCell for () {
14    fn from_cell(result_set: &ResultSet, row_idx: usize, column_idx: usize) -> GraphResult<Self> {
15        let scalar = result_set.get_scalar(row_idx, column_idx)?;
16        match scalar {
17            Scalar::Nil => Ok(()),
18            any => client_type_error!("failed to construct value: expected nil, found {:?}", any),
19        }
20    }
21}
22
23impl<T: FromCell> FromCell for Option<T> {
24    fn from_cell(result_set: &ResultSet, row_idx: usize, column_idx: usize) -> GraphResult<Self> {
25        let scalar = result_set.get_scalar(row_idx, column_idx)?;
26        match scalar {
27            Scalar::Nil => Ok(None),
28            _ => T::from_cell(result_set, row_idx, column_idx).map(Some),
29        }
30    }
31}
32
33impl FromCell for bool {
34    fn from_cell(result_set: &ResultSet, row_idx: usize, column_idx: usize) -> GraphResult<Self> {
35        let scalar = result_set.get_scalar(row_idx, column_idx)?;
36        match scalar {
37            Scalar::Boolean(boolean) => Ok(*boolean),
38            any => client_type_error!(
39                "failed to construct value: expected boolean, found {:?}",
40                any
41            ),
42        }
43    }
44}
45
46// The following code and macros produce the requisite type "magic" to allow
47// code in an actor to extract strongly-typed data from a result set in
48// tuples (or vecs of tuples)
49
50macro_rules! impl_from_scalar_for_integer {
51    ($t:ty) => {
52        impl FromCell for $t {
53            fn from_cell(
54                result_set: &ResultSet,
55                row_idx: usize,
56                column_idx: usize,
57            ) -> GraphResult<Self> {
58                let scalar = result_set.get_scalar(row_idx, column_idx)?;
59                match scalar {
60                    Scalar::Integer(int) => Ok(*int as $t),
61                    any => client_type_error!(
62                        "failed to construct value: expected integer, found {:?}",
63                        any
64                    ),
65                }
66            }
67        }
68    };
69}
70
71impl_from_scalar_for_integer!(u8);
72impl_from_scalar_for_integer!(u16);
73impl_from_scalar_for_integer!(u32);
74impl_from_scalar_for_integer!(u64);
75impl_from_scalar_for_integer!(usize);
76
77impl_from_scalar_for_integer!(i8);
78impl_from_scalar_for_integer!(i16);
79impl_from_scalar_for_integer!(i32);
80impl_from_scalar_for_integer!(i64);
81impl_from_scalar_for_integer!(isize);
82
83macro_rules! impl_from_scalar_for_float {
84    ($t:ty) => {
85        impl FromCell for $t {
86            fn from_cell(
87                result_set: &ResultSet,
88                row_idx: usize,
89                column_idx: usize,
90            ) -> GraphResult<Self> {
91                let scalar = result_set.get_scalar(row_idx, column_idx)?;
92                match scalar {
93                    Scalar::Double(double) => Ok(*double as $t),
94                    any => client_type_error!(
95                        "failed to construct value: expected double, found {:?}",
96                        any
97                    ),
98                }
99            }
100        }
101    };
102}
103
104impl_from_scalar_for_float!(f32);
105impl_from_scalar_for_float!(f64);
106
107impl FromCell for GraphString {
108    fn from_cell(result_set: &ResultSet, row_idx: usize, column_idx: usize) -> GraphResult<Self> {
109        let scalar = result_set.get_scalar(row_idx, column_idx)?;
110        match scalar {
111            Scalar::String(data) => Ok(data.clone()),
112            any => client_type_error!(
113                "failed to construct value: expected string, found {:?}",
114                any
115            ),
116        }
117    }
118}
119
120impl FromCell for String {
121    fn from_cell(result_set: &ResultSet, row_idx: usize, column_idx: usize) -> GraphResult<Self> {
122        let redis_string = GraphString::from_cell(result_set, row_idx, column_idx)?;
123        String::from_utf8(redis_string.into()).map_err(|_| GraphError::InvalidUtf8)
124    }
125}
126
127impl FromCell for Node {
128    fn from_cell(result_set: &ResultSet, row_idx: usize, column_idx: usize) -> GraphResult<Self> {
129        let node = result_set.get_node(row_idx, column_idx)?;
130        Ok(node.clone())
131    }
132}
133
134impl FromCell for Relation {
135    fn from_cell(result_set: &ResultSet, row_idx: usize, column_idx: usize) -> GraphResult<Self> {
136        let relation = result_set.get_relation(row_idx, column_idx)?;
137        Ok(relation.clone())
138    }
139}