streamweave_vec/consumers/vec_consumer.rs
1use streamweave::ConsumerConfig;
2use streamweave_error::ErrorStrategy;
3
4/// A consumer that collects items into a `Vec`.
5///
6/// This consumer collects all items from the stream into an internal `Vec`,
7/// preserving the order in which they were received.
8pub struct VecConsumer<T>
9where
10 T: std::fmt::Debug + Clone + Send + Sync + 'static,
11{
12 /// The internal `Vec` where items are collected.
13 pub vec: Vec<T>,
14 /// Configuration for the consumer, including error handling strategy.
15 pub config: ConsumerConfig<T>,
16}
17
18impl<T> Default for VecConsumer<T>
19where
20 T: std::fmt::Debug + Clone + Send + Sync + 'static,
21{
22 fn default() -> Self {
23 Self::new()
24 }
25}
26
27impl<T> VecConsumer<T>
28where
29 T: std::fmt::Debug + Clone + Send + Sync + 'static,
30{
31 /// Creates a new `VecConsumer` with an empty `Vec`.
32 pub fn new() -> Self {
33 Self {
34 vec: Vec::new(),
35 config: ConsumerConfig::default(),
36 }
37 }
38
39 /// Creates a new `VecConsumer` with a pre-allocated `Vec` capacity.
40 ///
41 /// # Arguments
42 ///
43 /// * `capacity` - The initial capacity of the internal `Vec`.
44 pub fn with_capacity(capacity: usize) -> Self {
45 Self {
46 vec: Vec::with_capacity(capacity),
47 config: ConsumerConfig::default(),
48 }
49 }
50
51 /// Sets the error handling strategy for this consumer.
52 ///
53 /// # Arguments
54 ///
55 /// * `strategy` - The error handling strategy to use.
56 pub fn with_error_strategy(mut self, strategy: ErrorStrategy<T>) -> Self {
57 self.config.error_strategy = strategy;
58 self
59 }
60
61 /// Sets the name for this consumer.
62 ///
63 /// # Arguments
64 ///
65 /// * `name` - The name to assign to this consumer.
66 pub fn with_name(mut self, name: String) -> Self {
67 self.config.name = name;
68 self
69 }
70
71 /// Consumes the consumer and returns the collected `Vec`.
72 ///
73 /// # Returns
74 ///
75 /// The `Vec` containing all collected items in order.
76 pub fn into_vec(self) -> Vec<T> {
77 self.vec
78 }
79}