#[doc(hidden)]
pub mod wit {
#![allow(missing_docs)]
use crate::wit_bindgen;
wit_bindgen::generate!({
runtime_path: "crate::wit_bindgen::rt",
world: "spin-sdk-redis",
path: "wit",
generate_all,
});
pub use spin::redis::redis;
}
use std::hash::{Hash, Hasher};
pub struct Connection(wit::redis::Connection);
pub use wit::redis::{Error, Payload, RedisParameter, RedisResult};
impl Connection {
pub async fn open(address: impl AsRef<str>) -> Result<Self, Error> {
wit::redis::Connection::open(address.as_ref().to_string())
.await
.map(Connection)
}
pub async fn publish(
&self,
channel: impl AsRef<str>,
payload: impl AsRef<[u8]>,
) -> Result<(), Error> {
self.0
.publish(channel.as_ref().to_string(), payload.as_ref().to_vec())
.await
}
pub async fn get(&self, key: impl AsRef<str>) -> Result<Option<Payload>, Error> {
self.0.get(key.as_ref().to_string()).await
}
pub async fn set(&self, key: impl AsRef<str>, value: impl AsRef<[u8]>) -> Result<(), Error> {
self.0
.set(key.as_ref().to_string(), value.as_ref().to_vec())
.await
}
pub async fn incr(&self, key: impl AsRef<str>) -> Result<i64, Error> {
self.0.incr(key.as_ref().to_string()).await
}
pub async fn del<Key: AsRef<str>>(
&self,
keys: impl IntoIterator<Item = Key>,
) -> Result<u32, Error> {
self.0
.del(
keys.into_iter()
.map(|key| key.as_ref().to_string())
.collect(),
)
.await
}
pub async fn sadd<Val: AsRef<str>>(
&self,
key: impl AsRef<str>,
values: impl IntoIterator<Item = Val>,
) -> Result<u32, Error> {
let values = values
.into_iter()
.map(|key| key.as_ref().to_string())
.collect();
self.0.sadd(key.as_ref().to_string(), values).await
}
pub async fn smembers(&self, key: impl AsRef<str>) -> Result<Vec<String>, Error> {
self.0.smembers(key.as_ref().to_string()).await
}
pub async fn srem<Val: AsRef<str>>(
&self,
key: impl AsRef<str>,
values: impl IntoIterator<Item = Val>,
) -> Result<u32, Error> {
let values = values
.into_iter()
.map(|key| key.as_ref().to_string())
.collect();
self.0.srem(key.as_ref().to_string(), values).await
}
pub async fn execute(
&self,
command: impl AsRef<str>,
arguments: impl IntoIterator<Item = RedisParameter>,
) -> Result<Vec<RedisResult>, Error> {
self.0
.execute(
command.as_ref().to_string(),
arguments.into_iter().collect(),
)
.await
}
}
impl PartialEq for RedisResult {
fn eq(&self, other: &Self) -> bool {
use RedisResult::*;
match (self, other) {
(Nil, Nil) => true,
(Status(a), Status(b)) => a == b,
(Int64(a), Int64(b)) => a == b,
(Binary(a), Binary(b)) => a == b,
_ => false,
}
}
}
impl Eq for RedisResult {}
impl Hash for RedisResult {
fn hash<H: Hasher>(&self, state: &mut H) {
use RedisResult::*;
match self {
Nil => (),
Status(s) => s.hash(state),
Int64(v) => v.hash(state),
Binary(v) => v.hash(state),
}
}
}