redis_asio/stream/
produce.rs

1use super::EntryId;
2use crate::{RedisCommand, IntoRedisArgument, command};
3use std::collections::HashMap;
4
5
6/// Set of options that are required by `RedisStream::send_entry()`
7#[derive(Clone)]
8pub struct SendEntryOptions {
9    /// Stream name
10    pub(crate) stream: String,
11    /// Optional explicit entry id
12    pub(crate) entry_id: Option<EntryId>,
13}
14
15impl SendEntryOptions {
16    pub fn new(stream: String) -> SendEntryOptions {
17        let entry_id: Option<EntryId> = None;
18        SendEntryOptions { stream, entry_id }
19    }
20
21    pub fn with_id(stream: String, entry_id: EntryId) -> SendEntryOptions {
22        let entry_id = Some(entry_id);
23        SendEntryOptions { stream, entry_id }
24    }
25}
26
27pub(crate) fn add_command<T>(options: SendEntryOptions, key_values: HashMap<String, T>) -> RedisCommand
28    where T: IntoRedisArgument {
29    let mut cmd = command("XADD").arg(options.stream);
30
31    match options.entry_id {
32        Some(entry_id) => cmd.arg_mut(entry_id.to_string()),
33        _ => cmd.arg_mut("*")
34    }
35
36    for (key, value) in key_values.into_iter() {
37        cmd.arg_mut(key);
38        cmd.arg_mut(value);
39    }
40
41    cmd
42}