cdk_common/pub_sub/
types.rs

1//! Pubsub Event definition
2//!
3//! The Pubsub Event defines the Topic struct and how an event can be converted to Topics.
4
5use std::hash::Hash;
6use std::sync::Arc;
7
8use serde::de::DeserializeOwned;
9use serde::Serialize;
10
11use super::Subscriber;
12
13/// Pubsub settings
14#[async_trait::async_trait]
15pub trait Spec: Send + Sync {
16    /// Topic
17    type Topic: Send
18        + Sync
19        + Clone
20        + Eq
21        + PartialEq
22        + Ord
23        + PartialOrd
24        + Hash
25        + Send
26        + Sync
27        + DeserializeOwned
28        + Serialize;
29
30    /// Event
31    type Event: Event<Topic = Self::Topic>
32        + Send
33        + Sync
34        + Eq
35        + PartialEq
36        + DeserializeOwned
37        + Serialize;
38
39    /// Subscription Id
40    type SubscriptionId: Clone
41        + Default
42        + Eq
43        + PartialEq
44        + Ord
45        + PartialOrd
46        + Hash
47        + Send
48        + Sync
49        + DeserializeOwned
50        + Serialize;
51
52    /// Create a new context
53    type Context;
54
55    /// Create a new instance from a given context
56    fn new_instance(context: Self::Context) -> Arc<Self>
57    where
58        Self: Sized;
59
60    /// Callback function that is called on new subscriptions, to back-fill optionally the previous
61    /// events
62    async fn fetch_events(
63        self: &Arc<Self>,
64        topics: Vec<<Self::Event as Event>::Topic>,
65        reply_to: Subscriber<Self>,
66    ) where
67        Self: Sized;
68}
69
70/// Event trait
71pub trait Event: Clone + Send + Sync + Eq + PartialEq + DeserializeOwned + Serialize {
72    /// Generic Topic
73    ///
74    /// It should be serializable/deserializable to be stored in the database layer and it should
75    /// also be sorted in a BTree for in-memory matching
76    type Topic;
77
78    /// To topics
79    fn get_topics(&self) -> Vec<Self::Topic>;
80}