use std::pin::Pin;
use futures::Stream;
use crate::{
model::InitializeRequestParams, transport::common::server_side_http::ServerSseMessage,
};
pub type EventId = String;
pub type StreamId = String;
pub type EventStream = Pin<Box<dyn Stream<Item = ServerSseMessage> + Send + Sync + 'static>>;
pub type EventStoreError = Box<dyn std::error::Error + Send + Sync + 'static>;
#[async_trait::async_trait]
pub trait EventStore: Send + Sync + 'static {
async fn store_event(
&self,
stream_id: &str,
event: &ServerSseMessage,
) -> Result<EventId, EventStoreError>;
async fn replay_events_after(
&self,
last_event_id: &str,
) -> Result<EventStream, EventStoreError>;
}
impl std::fmt::Debug for dyn EventStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("<EventStore>")
}
}
#[non_exhaustive]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct SessionState {
pub initialize_params: InitializeRequestParams,
}
impl SessionState {
pub fn new(initialize_params: InitializeRequestParams) -> Self {
Self { initialize_params }
}
}
pub type SessionStoreError = Box<dyn std::error::Error + Send + Sync + 'static>;
#[async_trait::async_trait]
pub trait SessionStore: Send + Sync + 'static {
async fn load(&self, session_id: &str) -> Result<Option<SessionState>, SessionStoreError>;
async fn store(&self, session_id: &str, state: &SessionState) -> Result<(), SessionStoreError>;
async fn delete(&self, session_id: &str) -> Result<(), SessionStoreError>;
}