use crate::{
Cache, CacheConnection, CacheRecord, CacheRepo, CacheRepoError, EventConnection, EventKind,
EventLog, EventRecord, EventRepo, EventRepoError, InterpidRepo, IntoCache, IntoEvent,
};
#[derive(Clone, Debug)]
pub struct IntrepidConnection<Repo> {
pub repo: Repo,
}
impl<Repo> IntrepidConnection<Repo>
where
Repo: InterpidRepo + Clone + Send + Sync + 'static,
{
pub fn new(repo: Repo) -> Self {
Self { repo }
}
pub fn event_connection(
&self,
target_stream_name: impl AsRef<str> + Send,
own_stream_name: impl AsRef<str> + Send,
) -> EventConnection<Repo> {
EventConnection::new(self.repo.clone(), target_stream_name, own_stream_name)
}
pub fn cache_connection(&self) -> CacheConnection<Repo> {
CacheConnection::new(self.repo.clone())
}
pub fn into_inner(self) -> Repo {
self.repo
}
}
#[async_trait::async_trait]
impl<Repo> CacheRepo for IntrepidConnection<Repo>
where
Repo: InterpidRepo + Send + Sync,
{
async fn get_memo(
&self,
uri: impl AsRef<str> + Send,
) -> Result<Cache<CacheRecord>, CacheRepoError> {
self.repo.get_memo(uri).await
}
async fn set_memo<CacheCandidate>(
&self,
record: CacheCandidate,
) -> Result<CacheRecord, CacheRepoError>
where
CacheCandidate: IntoCache + Send,
{
self.repo.set_memo(record).await
}
}
#[async_trait::async_trait]
impl<Repo> EventRepo for IntrepidConnection<Repo>
where
Repo: InterpidRepo + Send + Sync,
{
async fn entries_since_position(
&self,
stream_name: impl AsRef<str> + Send,
position: i64,
) -> Result<EventLog, EventRepoError> {
self.repo
.entries_since_position(stream_name, position)
.await
}
async fn publish<EventCandidate>(&self, event: EventCandidate) -> Result<(), EventRepoError>
where
EventCandidate: IntoEvent + Send,
{
self.repo.publish(event).await
}
async fn last(
&self,
stream_name: impl AsRef<str> + Send,
kind: EventKind,
) -> Option<EventRecord> {
self.repo.last(stream_name, kind).await
}
}