1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use crate::{Event, EventListeners, Eventbus, TopicKey};
use std::fmt::{Debug, Formatter};

/// A `Topic` wrapper for a `TopicKey`
pub struct Topic<T> {
    pub(crate) key: TopicKey,
    pub(crate) bus: Eventbus,
    pub(crate) event_listeners: EventListeners<T>,
}

impl<T> Topic<T> {
    /// create an event from message
    pub fn create_event(&self, message: T) -> Event<T> {
        Event::new(self.key.clone(), message)
    }

    /// get the key of a topic
    pub fn get_key(&self) -> &TopicKey {
        &self.key
    }

    /// get the associated eventbus
    pub fn get_bus(&self) -> &Eventbus {
        &self.bus
    }

    /// get event listeners subscribed to this topic
    pub fn get_listeners(&self) -> &EventListeners<T> {
        &self.event_listeners
    }
}

impl<T> Debug for Topic<T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct(format!("Topic<{}>", std::any::type_name::<T>()).as_str())
            .field("key", &self.key)
            .field("bus", &self.bus)
            .finish()
    }
}