Skip to content

Commit 60daaa7

Browse files
urklenihohit
authored andcommitted
fixup! Add xadd_options and xtrim_options
1 parent f644268 commit 60daaa7

File tree

2 files changed

+41
-29
lines changed

2 files changed

+41
-29
lines changed

redis/src/commands/mod.rs

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1423,47 +1423,36 @@ implement_commands! {
14231423

14241424
/// Add a stream message with options.
14251425
///
1426-
/// ```text
1427-
/// XADD key [NOMKSTREAM] [<MAXLEN|MINID> [~|=] threshold [LIMIT count]] <* | ID> field value [field value] ...
1426+
/// Items can be any list type, e.g.
1427+
/// ```rust
1428+
/// // static items
1429+
/// let items = &[("key", "val"), ("key2", "val2")];
1430+
/// # use std::collections::BTreeMap;
1431+
/// // A map (Can be BTreeMap, HashMap, etc)
1432+
/// let mut map: BTreeMap<&str, &str> = BTreeMap::new();
1433+
/// map.insert("ab", "cd");
1434+
/// map.insert("ef", "gh");
1435+
/// map.insert("ij", "kl");
14281436
/// ```
1429-
#[cfg(feature = "streams")]
1430-
#[cfg_attr(docsrs, doc(cfg(feature = "streams")))]
1431-
fn xadd_options<
1432-
K: ToRedisArgs, ID: ToRedisArgs, F: ToRedisArgs, V: ToRedisArgs
1433-
>(
1434-
key: K,
1435-
id: ID,
1436-
items: &'a [(F, V)],
1437-
options: &'a streams::StreamAddOptions
1438-
) {
1439-
cmd("XADD")
1440-
.arg(key)
1441-
.arg(options)
1442-
.arg(id)
1443-
.arg(items)
1444-
}
1445-
1446-
1447-
/// Add a stream message with options.
14481437
///
14491438
/// ```text
14501439
/// XADD key [NOMKSTREAM] [<MAXLEN|MINID> [~|=] threshold [LIMIT count]] <* | ID> field value [field value] ...
14511440
/// ```
14521441
#[cfg(feature = "streams")]
14531442
#[cfg_attr(docsrs, doc(cfg(feature = "streams")))]
1454-
fn xadd_map_options<
1455-
K: ToRedisArgs, ID: ToRedisArgs, BTM: ToRedisArgs
1443+
fn xadd_options<
1444+
K: ToRedisArgs, ID: ToRedisArgs, I: ToRedisArgs
14561445
>(
14571446
key: K,
14581447
id: ID,
1459-
map: BTM,
1448+
items: I,
14601449
options: &'a streams::StreamAddOptions
14611450
) {
14621451
cmd("XADD")
14631452
.arg(key)
14641453
.arg(options)
14651454
.arg(id)
1466-
.arg(map)
1455+
.arg(items)
14671456
}
14681457

14691458

redis/tests/test_streams.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,11 @@ fn test_xgroup_create() {
171171

172172
// no key exists... this call breaks the connection pipe for some reason
173173
let reply: RedisResult<StreamInfoStreamReply> = con.xinfo_stream("k10");
174-
assert!(reply.is_err());
174+
assert!(
175+
matches!(&reply, Err(e) if e.kind() == redis::ErrorKind::ResponseError
176+
&& e.code() == Some("ERR")
177+
&& e.detail() == Some("no such key"))
178+
);
175179

176180
// redo the connection because the above error
177181
con = ctx.connection();
@@ -426,14 +430,21 @@ fn test_xadd_options() {
426430
let ctx = TestContext::new();
427431
let mut con = ctx.connection();
428432

429-
// NoMKStream will cause this command to fail
430-
let result: RedisResult<String> = con.xadd_options(
433+
// NoMKStream will return a nil when the stream does not exist
434+
let result: RedisResult<redis::Value> = con.xadd_options(
431435
"k1",
432436
"*",
433437
&[("h", "w")],
434438
&StreamAddOptions::default().nomkstream(),
435439
);
436-
assert!(result.is_err());
440+
assert_eq!(result, Ok(redis::Value::Nil));
441+
442+
let result: RedisResult<StreamInfoStreamReply> = con.xinfo_stream("k1");
443+
assert!(
444+
matches!(&result, Err(e) if e.kind() == redis::ErrorKind::ResponseError
445+
&& e.code() == Some("ERR")
446+
&& e.detail() == Some("no such key"))
447+
);
437448

438449
fn setup_stream(con: &mut Connection) {
439450
let _: RedisResult<()> = con.del("k1");
@@ -475,6 +486,18 @@ fn test_xadd_options() {
475486
let info: StreamInfoStreamReply = con.xinfo_stream("k1").unwrap();
476487
assert_eq!(info.length, 6);
477488
assert_eq!(info.first_entry.id, "1-5");
489+
490+
// test adding from a map
491+
let mut map: BTreeMap<&str, &str> = BTreeMap::new();
492+
map.insert("ab", "cd");
493+
map.insert("ef", "gh");
494+
map.insert("ij", "kl");
495+
let _: RedisResult<String> = con.xadd_options("k1", "3-1", map, &StreamAddOptions::default());
496+
497+
let info: StreamInfoStreamReply = con.xinfo_stream("k1").unwrap();
498+
assert_eq!(info.length, 7);
499+
assert_eq!(info.first_entry.id, "1-5");
500+
assert_eq!(info.last_entry.id, "3-1");
478501
}
479502

480503
#[test]

0 commit comments

Comments
 (0)