intrepid-model 0.3.0

Manage complex async business logic with ease
Documentation
use crate::{Event, EventKind};

/// A marker struct to track subscription read progress.
#[derive(Clone, Debug, serde::Serialize, serde::Deserialize)]
pub struct SubscriptionMarker {
    /// The name of the stream that the read marker focuses on. Stream markers are
    /// tracked in the stream of the subscriber, meaning a single subscriber can track
    /// multiple target streams.
    pub target_name: String,
    /// The position of the last read event in the target stream.
    pub position: i64,
}

impl SubscriptionMarker {
    /// Create a new read marker event.
    pub fn read_marker(
        stream_name: impl AsRef<str>,
        target_stream_name: impl AsRef<str>,
        position: i64,
    ) -> Event {
        Event {
            stream_name: stream_name.as_ref().to_owned(),
            kind: EventKind::Marker,
            // TODO: Can this be something other than serde_json? It's a bit heavy for this.
            data: serde_json::to_vec(&SubscriptionMarker {
                target_name: target_stream_name.as_ref().to_owned(),
                position,
            })
            .expect("failed to serialize event log read marker")
            .into(),
        }
    }
}