use async_trait::async_trait;
use futures::Stream;
use std::pin::Pin;
use thiserror::Error;
use zopp_storage::EnvironmentId;
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EventType {
Created,
Updated,
Deleted,
}
#[derive(Clone, Debug)]
pub struct SecretChangeEvent {
pub event_type: EventType,
pub key: String,
pub version: i64,
pub timestamp: i64,
}
#[derive(Debug, Error)]
pub enum EventBusError {
#[error("backend error: {0}")]
Backend(String),
}
pub type EventStream = Pin<Box<dyn Stream<Item = SecretChangeEvent> + Send>>;
#[async_trait]
pub trait EventBus: Send + Sync {
async fn publish(
&self,
env_id: &EnvironmentId,
event: SecretChangeEvent,
) -> Result<(), EventBusError>;
async fn subscribe(&self, env_id: &EnvironmentId) -> Result<EventStream, EventBusError>;
}