redisgraphio/
helpers.rs

1use redis::{RedisResult, ErrorKind, RedisError};
2
3use crate::{FromGraphValue, GraphValue};
4
5
6/// Helper macro to apply a macro to each following type
7macro_rules! apply_macro {
8    ($m:tt, $($x:ty),+) => {
9        $(
10            $m!($x);
11        )*
12    };
13}
14
15pub(crate) use apply_macro;
16
17/// Shorthand for FromGraphValue::from_graph_value(value)
18#[inline(always)]
19pub fn from_graph_value<T: FromGraphValue>(value: GraphValue) -> RedisResult<T> {
20    FromGraphValue::from_graph_value(value)
21}
22
23/// Helper for creating a Rediserror
24pub fn create_rediserror(desc: &str) -> RedisError {
25    (
26        ErrorKind::TypeError,
27        "Parsing Error",
28        desc.to_owned()
29    ).into()
30}
31
32/// Macro for creating a GraphQuery
33/// ## Diffrent usecases
34/// ```
35/// query!("query string"); // Normal query
36/// query!("query string", true); // Normal read only query
37/// query!(
38///     "query string $param",
39///     {
40///         "param" => 5 // or "Some string" or 4.8 everything is converted with Parameter::from
41///     }
42/// ); // Query with parameters and read only
43/// query!(
44///     "query string $param $name",
45///     {
46///         "param" => 5,
47///         "name" => "username"
48///     },
49///     true
50/// ); // Query with parameters and read only
51/// ```
52#[macro_export]
53macro_rules! query {
54    ( $s:expr $(, $ro:literal)?) => {{
55        #[allow(unused_assignments, unused_mut)]
56        let mut read_only = false;
57        $(
58            read_only = $ro;
59        )?
60        $crate::GraphQuery {
61            query: $s, read_only, params: vec![]
62        }
63    }};
64    ( $s:expr, { $( $k:expr => $v:expr ),* } $(, $ro:literal)?) => {{
65        #[allow(unused_assignments, unused_mut)]
66        let mut read_only = false;
67        $(
68            read_only = $ro;
69        )?
70        $crate::GraphQuery {
71            query: $s, read_only, params: vec![$(
72                ($k, $crate::Parameter::from($v)),
73            )*]
74        }
75    }}
76}