Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Threated committed Jun 13, 2022
1 parent 56c1cf5 commit 35f606d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 95 deletions.
2 changes: 1 addition & 1 deletion src/aio.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use redis::{aio::ConnectionLike, RedisFuture, cmd, RedisResult};
use redis::{aio::ConnectionLike, RedisFuture, cmd};

use crate::{types::GraphQuery, FromGraphValue, GraphResponse, query};

Expand Down
13 changes: 12 additions & 1 deletion src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ use redis::{RedisResult, ErrorKind, RedisError};
use crate::{FromGraphValue, GraphValue};


/// Helper macro to apply a macro to each following type
macro_rules! apply_macro {
($m:tt, $($x:ty),+) => {
$(
$m!($x);
)*
};
}

pub(crate) use apply_macro;

/// So you dont have to write FromGraphValue::from_graph_value(value) every time
#[inline(always)]
pub fn from_graph_value<T: FromGraphValue>(value: GraphValue) -> RedisResult<T> {
Expand Down Expand Up @@ -38,7 +49,7 @@ macro_rules! query {
)?
crate::types::GraphQuery {
query: $s, read_only, params: vec![$(
($k, crate::parse::Parameter::from($v)),
($k, crate::types::Parameter::from($v)),
)*]
}
}}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod tests;
pub use crate::types::*;
pub use crate::sync::GraphCommands;
pub use crate::parse::*;
pub use crate::helpers::from_graph_value;
pub use crate::helpers::{from_graph_value, create_rediserror};

#[cfg(any(feature = "tokio-comp", feature = "async-std-comp"))]
mod aio;
Expand Down
130 changes: 39 additions & 91 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ use std::{
ops::{Deref, DerefMut},
};

use crate::{from_graph_value, helpers::create_rediserror};
use crate::{
from_graph_value,
helpers::{create_rediserror, apply_macro}
};

/// Official enum from redis-graph https://github.com/RedisGraph/RedisGraph/blob/master/src/resultset/formatters/resultset_formatter.h#L20-L33
mod types {
Expand Down Expand Up @@ -182,28 +185,10 @@ pub struct GraphPath {
pub relationships: Vec<Relationship>,
}

impl FromRedisValue for GraphPath {
fn from_redis_value(v: &Value) -> RedisResult<Self> {
let (nodes, relationships): (GraphValue, GraphValue) = from_redis_value(v)?;
Ok(GraphPath {
nodes: from_graph_value(nodes)?,
relationships: from_graph_value(relationships)?,
})
}
}

pub trait FromGraphValue: Sized {
fn from_graph_value(value: GraphValue) -> RedisResult<Self>;
}

/// Helper macro to apply a macro to each following type
macro_rules! apply_macro {
($m:tt, $($x:ty),+) => {
$(
$m!($x);
)*
};
}

/// Macro for implementing the FromGraphValue Trait for a int type
macro_rules! from_graph_value_for_int {
Expand All @@ -221,6 +206,7 @@ macro_rules! from_graph_value_for_int {
}
};
}

/// Macro for implementing the FromGraphValue Trait for a float type
macro_rules! from_graph_value_for_float {
( $t:ty ) => {
Expand Down Expand Up @@ -319,6 +305,30 @@ impl FromGraphValue for GeoPoint {
}
}

impl FromGraphValue for Node {
fn from_graph_value(value: GraphValue) -> RedisResult<Self> {
match value {
GraphValue::Node(node) => Ok(node),
_ => Err(create_rediserror(&format!(
"Cant convert {:?} to Node",
value
))),
}
}
}

impl FromGraphValue for Relationship {
fn from_graph_value(value: GraphValue) -> RedisResult<Self> {
match value {
GraphValue::Relation(rel) => Ok(rel),
_ => Err(create_rediserror(&format!(
"Cant convert {:?} to Relationship",
value
))),
}
}
}

impl<T: FromGraphValue> FromGraphValue for Option<T> {
fn from_graph_value(value: GraphValue) -> RedisResult<Self> {
match value {
Expand Down Expand Up @@ -405,6 +415,16 @@ impl FromRedisValue for GraphValue {
}
}

impl FromRedisValue for GraphPath {
fn from_redis_value(v: &Value) -> RedisResult<Self> {
let (nodes, relationships): (GraphValue, GraphValue) = from_redis_value(v)?;
Ok(GraphPath {
nodes: from_graph_value(nodes)?,
relationships: from_graph_value(relationships)?,
})
}
}

impl FromRedisValue for GeoPoint {
fn from_redis_value(v: &Value) -> RedisResult<Self> {
let (latitude, longitude): (f32, f32) = from_redis_value(v)?;
Expand Down Expand Up @@ -446,29 +466,6 @@ impl FromRedisValue for Node {
}
}

impl FromGraphValue for Node {
fn from_graph_value(value: GraphValue) -> RedisResult<Self> {
match value {
GraphValue::Node(node) => Ok(node),
_ => Err(create_rediserror(&format!(
"Cant convert {:?} to Node",
value
))),
}
}
}

impl FromGraphValue for Relationship {
fn from_graph_value(value: GraphValue) -> RedisResult<Self> {
match value {
GraphValue::Relation(rel) => Ok(rel),
_ => Err(create_rediserror(&format!(
"Cant convert {:?} to Relationship",
value
))),
}
}
}

impl FromRedisValue for Relationship {
fn from_redis_value(v: &Value) -> RedisResult<Self> {
Expand Down Expand Up @@ -535,52 +532,3 @@ fn convert_to_graphvalue(type_: i64, val: &Value) -> RedisResult<GraphValue> {
VALUE_UNKNOWN | _ => Ok(GraphValue::Unknown(val.to_owned())),
}
}

#[derive(Clone, PartialEq, Debug)]
pub enum Parameter {
String(String),
Int(i64),
Double(f64),
}

/// Macro for implementing the From Trait for a numeric type
macro_rules! parameter_from_int {
( $t:ty ) => {
impl From<$t> for Parameter {
fn from(id: $t) -> Self {
Parameter::Int(i64::from(id))
}
}
};
}

macro_rules! parameter_from_double {
( $t:ty ) => {
impl From<$t> for Parameter {
fn from(id: $t) -> Self {
Parameter::Double(f64::from(id))
}
}
};
}

apply_macro!(parameter_from_int, i8, i16, i32, i64, u8, u16, u32);
apply_macro!(parameter_from_double, f32, f64);

impl<'a> From<&'a str> for Parameter {
fn from(string: &'a str) -> Self {
Parameter::String(string.to_string())
}
}

impl From<String> for Parameter {
fn from(string: String) -> Self {
Parameter::String(string)
}
}

impl From<&String> for Parameter {
fn from(string: &String) -> Self {
Parameter::String(string.to_string())
}
}
51 changes: 50 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use redis::{FromRedisValue, Value, RedisResult, from_redis_value};

use crate::{GraphValue, helpers::create_rediserror, FromGraphValue, from_graph_value, Parameter};
use crate::{GraphValue, helpers::{create_rediserror, apply_macro}, FromGraphValue, from_graph_value};

#[derive(Debug)]
pub struct GraphResponse<T = GraphValue> where T: FromGraphValue {
Expand Down Expand Up @@ -108,3 +108,52 @@ impl From<&'static str> for GraphQuery {
GraphQuery { query, params: vec![], read_only: false}
}
}

#[derive(Clone, PartialEq, Debug)]
pub enum Parameter {
String(String),
Int(i64),
Double(f64),
}

/// Macro for implementing the From Trait for a numeric type
macro_rules! parameter_from_int {
( $t:ty ) => {
impl From<$t> for Parameter {
fn from(id: $t) -> Self {
Parameter::Int(i64::from(id))
}
}
};
}

macro_rules! parameter_from_double {
( $t:ty ) => {
impl From<$t> for Parameter {
fn from(id: $t) -> Self {
Parameter::Double(f64::from(id))
}
}
};
}

apply_macro!(parameter_from_int, i8, i16, i32, i64, u8, u16, u32);
apply_macro!(parameter_from_double, f32, f64);

impl<'a> From<&'a str> for Parameter {
fn from(string: &'a str) -> Self {
Parameter::String(string.to_string())
}
}

impl From<String> for Parameter {
fn from(string: String) -> Self {
Parameter::String(string)
}
}

impl From<&String> for Parameter {
fn from(string: &String) -> Self {
Parameter::String(string.to_string())
}
}

0 comments on commit 35f606d

Please sign in to comment.