use std::future::Future;
use super::instance::Instance;
use super::{CallContext, ContextError, ContextValue, OperationDeclaration, Response};
use crate::operations::streams::Publish;
use crate::{Message, StreamSpec};
pub trait Host: Send + Sync + Sized {
type Error: std::error::Error + Send + Sync + 'static;
type Instance<O: OperationDeclaration>: Instance<O, Error = Self::Error>;
fn context(&self) -> &CallContext;
fn with_value<T: ContextValue>(&self, value: T) -> Result<Self, ContextError>;
fn submit<O: OperationDeclaration>(&self, request: O) -> impl Future<Output = Result<Self::Instance<O>, Self::Error>> + Send;
fn call<O: OperationDeclaration>(&self, request: O) -> impl Future<Output = Result<Response<O::Response>, Self::Error>> + Send;
fn submit_with_resources<O: OperationDeclaration<Response = ()>, R: Send + Sync + 'static>(
&self, request: O, resources: std::sync::Arc<R>,
) -> impl Future<Output = Result<Self::Instance<O>, Self::Error>> + Send;
fn publish_on<S: StreamSpec>(&self, row: &S::Row) -> impl Future<Output = Result<(), Self::Error>> + Send
where
S::Row: Message,
{
let published = self.call(Publish {
stream: S::ID.to_owned(),
schema: S::VERSION,
sequence: None,
published_unix_micros: None,
context: None,
payload: row.encode_to_vec(),
following: Vec::new(),
});
async move {
published.await?;
Ok(())
}
}
fn publish<S: StreamSpec<Row = S> + Message>(&self, row: &S) -> impl Future<Output = Result<(), Self::Error>> + Send {
self.publish_on::<S>(row)
}
}