kevy_client_async/
cmd_hash.rs1use std::io;
4
5use kevy_resp::Reply;
6
7use crate::conn::AsyncConnection;
8use crate::reply::{array_to_bulks, string, unexpected, vec2, vec3};
9
10impl AsyncConnection {
11 pub async fn hset(
14 &mut self,
15 key: &[u8],
16 pairs: &[(&[u8], &[u8])],
17 ) -> io::Result<usize> {
18 let mut args = Vec::with_capacity(2 + pairs.len() * 2);
19 args.push(b"HSET".to_vec());
20 args.push(key.to_vec());
21 for (f, v) in pairs {
22 args.push(f.to_vec());
23 args.push(v.to_vec());
24 }
25 match self.codec_mut().request(&args).await? {
26 Reply::Int(n) if n >= 0 => Ok(n as usize),
27 Reply::Error(e) => Err(io::Error::other(string(e))),
28 other => Err(unexpected(other)),
29 }
30 }
31
32 pub async fn hget(&mut self, key: &[u8], field: &[u8]) -> io::Result<Option<Vec<u8>>> {
34 match self.codec_mut().request(&vec3(b"HGET", key, field)).await? {
35 Reply::Bulk(v) => Ok(Some(v)),
36 Reply::Nil => Ok(None),
37 Reply::Error(e) => Err(io::Error::other(string(e))),
38 other => Err(unexpected(other)),
39 }
40 }
41
42 pub async fn hdel(&mut self, key: &[u8], fields: &[&[u8]]) -> io::Result<usize> {
44 let mut args = Vec::with_capacity(fields.len() + 2);
45 args.push(b"HDEL".to_vec());
46 args.push(key.to_vec());
47 args.extend(fields.iter().map(|f| f.to_vec()));
48 match self.codec_mut().request(&args).await? {
49 Reply::Int(n) if n >= 0 => Ok(n as usize),
50 Reply::Error(e) => Err(io::Error::other(string(e))),
51 other => Err(unexpected(other)),
52 }
53 }
54
55 pub async fn hlen(&mut self, key: &[u8]) -> io::Result<usize> {
57 match self.codec_mut().request(&vec2(b"HLEN", key)).await? {
58 Reply::Int(n) if n >= 0 => Ok(n as usize),
59 Reply::Error(e) => Err(io::Error::other(string(e))),
60 other => Err(unexpected(other)),
61 }
62 }
63
64 pub async fn hgetall(&mut self, key: &[u8]) -> io::Result<Vec<Vec<u8>>> {
66 match self.codec_mut().request(&vec2(b"HGETALL", key)).await? {
67 Reply::Array(items) => array_to_bulks(items),
68 Reply::Error(e) => Err(io::Error::other(string(e))),
69 other => Err(unexpected(other)),
70 }
71 }
72
73 pub async fn hkeys(&mut self, key: &[u8]) -> io::Result<Vec<Vec<u8>>> {
75 match self.codec_mut().request(&vec2(b"HKEYS", key)).await? {
76 Reply::Array(items) => array_to_bulks(items),
77 Reply::Error(e) => Err(io::Error::other(string(e))),
78 other => Err(unexpected(other)),
79 }
80 }
81
82 pub async fn hvals(&mut self, key: &[u8]) -> io::Result<Vec<Vec<u8>>> {
84 match self.codec_mut().request(&vec2(b"HVALS", key)).await? {
85 Reply::Array(items) => array_to_bulks(items),
86 Reply::Error(e) => Err(io::Error::other(string(e))),
87 other => Err(unexpected(other)),
88 }
89 }
90}