Skip to content

Commit 074066a

Browse files
committed
Add Keys command #4
1 parent 874f06a commit 074066a

File tree

4 files changed

+66
-5
lines changed

4 files changed

+66
-5
lines changed

CHANGES.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Changes
22

3-
## [0.3.2] - 2022-05-xx
3+
## [0.3.2] - 2022-05-04
4+
5+
* Add `Keys` command #4
46

57
* Add Clone trait impl to CommandError
68

src/cmd/keys.rs

+58-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
use std::convert::{TryFrom, TryInto};
2+
3+
use ntex::util::ByteString;
4+
15
use super::{utils, Command, CommandError};
26
use crate::codec::{BulkString, Request, Response};
3-
use std::convert::TryFrom;
47

58
/// DEL redis command
69
///
@@ -183,3 +186,57 @@ impl Command for TtlCommand {
183186
})
184187
}
185188
}
189+
190+
/// KEYS redis command
191+
///
192+
/// Returns all keys matching pattern.
193+
///
194+
/// ```rust
195+
/// use ntex_redis::{cmd, RedisConnector};
196+
/// # use rand::{thread_rng, Rng, distributions::Alphanumeric};
197+
/// # fn gen_random_key() -> String {
198+
/// # thread_rng().sample_iter(&Alphanumeric).take(12).map(char::from).collect::<String>()
199+
/// # }
200+
///
201+
/// #[ntex::main]
202+
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
203+
/// let redis = RedisConnector::new("127.0.0.1:6379").connect().await?;
204+
///
205+
/// // set keys
206+
/// redis.exec(cmd::Set("firstname", "Jack")).await?;
207+
/// redis.exec(cmd::Set("lastname", "Stuntman")).await?;
208+
///
209+
/// // get keys
210+
/// let mut keys = redis.exec(cmd::Keys("*name*")).await?;
211+
/// # keys.sort();
212+
///
213+
/// assert_eq!(&keys[..], &["firstname", "lastname"][..]);
214+
/// Ok(())
215+
/// }
216+
/// ```
217+
pub fn Keys<T>(key: T) -> KeysPatternCommand
218+
where
219+
BulkString: From<T>,
220+
{
221+
KeysPatternCommand(Request::Array(vec![
222+
Request::from_static("KEYS"),
223+
Request::BulkString(key.into()),
224+
]))
225+
}
226+
227+
pub struct KeysPatternCommand(Request);
228+
229+
impl Command for KeysPatternCommand {
230+
type Output = Vec<ByteString>;
231+
232+
fn to_request(self) -> Request {
233+
self.0
234+
}
235+
236+
fn to_output(val: Response) -> Result<Self::Output, CommandError> {
237+
match val.try_into() {
238+
Ok(val) => Ok(val),
239+
Err((_, val)) => Err(CommandError::Output("Cannot parse response", val)),
240+
}
241+
}
242+
}

src/cmd/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ mod utils;
1515
pub use self::auth::Auth;
1616
pub use self::connection::{Ping, Select};
1717
pub use self::hashes::{HDel, HGet, HGetAll, HIncrBy, HLen, HSet};
18-
pub use self::keys::{Del, Exists, Expire, ExpireAt, Ttl, TtlResult};
18+
pub use self::keys::{Del, Exists, Expire, ExpireAt, Keys, Ttl, TtlResult};
1919
pub use self::lists::{LIndex, LPop, LPush, RPop, RPush};
2020
pub use self::strings::{Get, IncrBy, Set};
2121

@@ -35,7 +35,7 @@ pub mod commands {
3535
//! Command implementations
3636
pub use super::auth::AuthCommand;
3737
pub use super::hashes::{HDelCommand, HGetAllCommand, HSetCommand};
38-
pub use super::keys::{KeysCommand, TtlCommand};
38+
pub use super::keys::{KeysCommand, KeysPatternCommand, TtlCommand};
3939
pub use super::lists::LPushCommand;
4040
pub use super::strings::SetCommand;
4141
pub use super::utils::{BulkOutputCommand, IntOutputCommand};

src/errors.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,15 @@ impl From<Either<Error, io::Error>> for Error {
4444
}
4545
}
4646

47-
#[derive(Debug, Display, From)]
47+
#[derive(Debug, Display, From, Clone)]
4848
/// Redis connectivity errors
4949
pub enum ConnectError {
5050
/// Auth command failed
5151
Unauthorized,
52+
5253
/// Command execution error
5354
Command(CommandError),
55+
5456
/// Io connectivity error
5557
Connect(connect::ConnectError),
5658
}

0 commit comments

Comments
 (0)