Skip to main content

intrepid_model/subscriptions/
subscription_marker.rs

1use crate::{Event, EventKind};
2
3/// A marker struct to track subscription read progress.
4#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
5pub struct SubscriptionMarker {
6    /// The name of the stream that the read marker focuses on. Stream markers are
7    /// tracked in the stream of the subscriber, meaning a single subscriber can track
8    /// multiple target streams.
9    pub target_name: String,
10    /// The position of the last read event in the target stream.
11    pub position: i64,
12}
13
14impl SubscriptionMarker {
15    /// Create a new read marker event.
16    pub fn read_marker(
17        stream_name: impl AsRef<str>,
18        target_stream_name: impl AsRef<str>,
19        position: i64,
20    ) -> Event {
21        Event {
22            stream_name: stream_name.as_ref().to_owned(),
23            kind: EventKind::Marker,
24            // TODO: Can this be something other than serde_json? It's a bit heavy for this.
25            data: serde_json::to_vec(&SubscriptionMarker {
26                target_name: target_stream_name.as_ref().to_owned(),
27                position,
28            })
29            .expect("failed to serialize event log read marker")
30            .into(),
31        }
32    }
33}