Skip to main content

ara_com/
event.rs

1use crate::error::AraComError;
2use crate::types::*;
3use futures_core::Stream;
4use std::pin::Pin;
5
6/// Subscription state for event consumers
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub enum SubscriptionState {
9    /// Not subscribed
10    Idle,
11    /// Subscription requested, waiting for ack
12    Pending,
13    /// Actively subscribed
14    Subscribed,
15    /// Subscription was rejected or lost
16    Failed(String),
17}
18
19/// Configuration for event subscriptions
20#[derive(Debug, Clone)]
21pub struct EventConfig {
22    pub event_group_id: EventGroupId,
23    /// Max number of buffered events before dropping
24    pub buffer_size: usize,
25}
26
27impl EventConfig {
28    /// Create a new `EventConfig` with sensible defaults (buffer size = 64).
29    pub fn new(event_group_id: EventGroupId) -> Self {
30        Self {
31            event_group_id,
32            buffer_size: 64,
33        }
34    }
35}
36
37/// A stream of events from a subscribed event group
38pub type EventStream<T> = Pin<Box<dyn Stream<Item = Result<T, AraComError>> + Send>>;
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43
44    #[test]
45    fn test_event_config_new() {
46        let cfg = EventConfig::new(EventGroupId(0x0010));
47        assert_eq!(cfg.event_group_id, EventGroupId(0x0010));
48        assert_eq!(cfg.buffer_size, 64);
49    }
50
51    #[test]
52    fn test_subscription_state_equality() {
53        assert_eq!(SubscriptionState::Idle, SubscriptionState::Idle);
54        assert_ne!(SubscriptionState::Idle, SubscriptionState::Subscribed);
55        assert_eq!(
56            SubscriptionState::Failed("oops".into()),
57            SubscriptionState::Failed("oops".into())
58        );
59    }
60}