use std::{future::Future, pin::Pin, sync::Arc};
use bytes::Bytes;
use rustack_ses_model::{error::SesError, operations::SesOperation};
use crate::body::SesResponseBody;
pub trait SesHandler: Send + Sync + 'static {
fn handle_operation(
&self,
op: SesOperation,
body: Bytes,
) -> Pin<Box<dyn Future<Output = Result<http::Response<SesResponseBody>, SesError>> + Send>>;
fn handle_v2_operation(
&self,
method: http::Method,
path: String,
body: Bytes,
) -> Pin<Box<dyn Future<Output = Result<http::Response<SesResponseBody>, SesError>> + Send>>;
fn query_emails(&self, filter_id: Option<&str>, filter_source: Option<&str>) -> String;
fn clear_emails(&self, filter_id: Option<&str>);
}
pub async fn dispatch_operation<H: SesHandler>(
handler: &H,
op: SesOperation,
body: Bytes,
) -> Result<http::Response<SesResponseBody>, SesError> {
tracing::debug!(operation = %op, "dispatching SES operation");
handler.handle_operation(op, body).await
}
#[derive(Debug, Clone)]
pub struct SesHandlerRef<H: SesHandler> {
inner: Arc<H>,
}
impl<H: SesHandler> SesHandlerRef<H> {
pub fn new(handler: Arc<H>) -> Self {
Self { inner: handler }
}
#[must_use]
pub fn handler(&self) -> &H {
&self.inner
}
}