Skip to content

Commit

Permalink
refact types
Browse files Browse the repository at this point in the history
  • Loading branch information
aamalev committed Nov 28, 2022
1 parent 49017a7 commit 4860604
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
22 changes: 3 additions & 19 deletions src/cluster.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use crate::types;
use deadpool_redis_cluster::{
redis::{aio::ConnectionLike, Value},
Config, Pool, Runtime,
};
use deadpool_redis_cluster::{redis::aio::ConnectionLike, Config, Pool, Runtime};
use pyo3::{
prelude::*,
types::{PyBytes, PyDict, PyList},
types::{PyBytes, PyDict},
};
use pyo3_asyncio::tokio::future_into_py;
use redis_cluster_async::redis::FromRedisValue;
Expand All @@ -29,19 +26,6 @@ async fn execute<T: FromRedisValue>(
Ok(result)
}

fn to_object(py: Python, value: Value) -> PyObject {
match value {
Value::Data(d) => PyBytes::new(py, &d).to_object(py),
Value::Nil => py.None(),
Value::Int(i) => i.to_object(py),
Value::Bulk(bulk) => {
PyList::new(py, bulk.into_iter().map(|v| to_object(py, v))).to_object(py)
}
Value::Status(s) => s.to_object(py),
Value::Okay => true.to_object(py),
}
}

#[pymethods]
impl Client {
#[new]
Expand Down Expand Up @@ -69,7 +53,7 @@ impl Client {
.req_packed_command(&redis_cluster_async::redis::cmd(&cmd.to_string()).arg(&args))
.await
.unwrap();
Ok(Python::with_gil(|py| to_object(py, value)))
Ok(Python::with_gil(|py| types::to_object(py, value)))
})
}

Expand Down
20 changes: 18 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
use deadpool_redis_cluster::redis::{RedisWrite, ToRedisArgs};
use pyo3::FromPyObject;
use deadpool_redis_cluster::redis::{RedisWrite, ToRedisArgs, Value};
use pyo3::{
types::{PyBytes, PyList},
FromPyObject, PyObject, Python, ToPyObject,
};

pub fn to_object(py: Python, value: Value) -> PyObject {
match value {
Value::Data(d) => PyBytes::new(py, &d).to_object(py),
Value::Nil => py.None(),
Value::Int(i) => i.to_object(py),
Value::Bulk(bulk) => {
PyList::new(py, bulk.into_iter().map(|v| to_object(py, v))).to_object(py)
}
Value::Status(s) => s.to_object(py),
Value::Okay => true.to_object(py),
}
}

#[derive(FromPyObject)]
pub enum Cmd {
Expand Down

0 comments on commit 4860604

Please sign in to comment.