redis_stream/
types.rs

1//! Defines types to use with the consumer commands.
2
3#[derive(Clone, Debug)]
4pub enum StartPosition {
5  EndOfStream,
6  Other(String),
7  StartOfStream,
8}
9
10/// Builder options for [`Consumer::init`].
11///
12/// Configuration settings for stream consumers (simple or group).
13///
14/// # Basic usage
15///
16/// ```
17/// use redis_stream::consumer::{ConsumerOpts, StartPosition};
18///
19/// let opts = ConsumerOpts::default().start_pos(StartPosition::StartOfStream);
20/// ```
21///
22/// # Group consumer
23///
24/// Specifying a `group` (with `group_name` and `consumer_name`), will instruct
25/// the [`Consumer`] to use consumer groups specific commands (like `XGROUP
26/// CREATE`, `XREADGROUP` or `XACK`).
27///
28/// ```
29/// use redis_stream::consumer::{ConsumerOpts, StartPosition};
30///
31/// let opts = ConsumerOpts::default()
32///   .group("my-group", "consumer.1")
33///   .start_pos(StartPosition::StartOfStream);
34/// ```
35/// [`Consumer`]: ../consumer/struct.Consumer.html
36/// [`Consumer::init`]:../consumer/struct.Consumer.html#method.init
37#[derive(Debug)]
38pub struct ConsumerOpts {
39  pub count: Option<usize>,
40  pub create_stream_if_not_exists: bool,
41  pub group: Option<(String, String)>,
42  pub process_pending: bool,
43  pub start_pos: StartPosition,
44  pub timeout: usize,
45}
46
47impl Default for ConsumerOpts {
48  fn default() -> Self {
49    Self {
50      count: None,
51      create_stream_if_not_exists: true,
52      group: None,
53      process_pending: true,
54      start_pos: StartPosition::EndOfStream,
55      timeout: 2_000,
56    }
57  }
58}
59
60impl ConsumerOpts {
61  /// Maximum number of message to read from the stream in one batch
62  pub fn count(mut self, count: usize) -> Self {
63    self.count = Some(count);
64    self
65  }
66
67  /// Create the stream in Redis before registering the group (default: `true`).
68  pub fn create_stream_if_not_exists(mut self, create_stream_if_not_exists: bool) -> Self {
69    self.create_stream_if_not_exists = create_stream_if_not_exists;
70    self
71  }
72
73  /// Name of the group and consumer. Enables Redis group consumer behavior if
74  /// specified
75  pub fn group(mut self, group_name: &str, consumer_name: &str) -> Self {
76    self.group = Some((group_name.to_string(), consumer_name.to_string()));
77    self
78  }
79
80  /// Start by processing pending messages before switching to real time data
81  /// (default: `true`)
82  pub fn process_pending(mut self, process_pending: bool) -> Self {
83    self.process_pending = process_pending;
84    self
85  }
86
87  /// Where to start reading messages in the stream.
88  pub fn start_pos(mut self, start_pos: StartPosition) -> Self {
89    self.start_pos = start_pos;
90    self
91  }
92
93  /// Maximum ms duration to block waiting for messages.
94  pub fn timeout(mut self, timeout: usize) -> Self {
95    self.timeout = timeout;
96    self
97  }
98}