harmos_sidecar/kit/
observer_host.rs1use std::future::Future;
18use std::sync::Arc;
19use super::{CallError, ObserverContext, PublicationError, Supervisor};
23use harmos_domain::{Message, StreamSpec, inventory::publications};
24
25pub trait ObserverHost: Send + Sync + Sized + 'static {
38 fn bind(resources: Arc<harmos_sdk::ExecutionContext>, supervisor: Supervisor) -> Self;
39}
40
41pub trait ObserverPublisher<V: Send>: Send + Sync + Sized {
42 type Error: std::error::Error + From<PublicationError> + Send + Sync + 'static;
43 const STREAM: Option<publications::Entry> = None;
44
45 fn publish(&self, context: &ObserverContext<'_, Self>, value: V) -> impl Future<Output = Result<(), Self::Error>> + Send;
46}
47
48pub trait ObserverStreamPublisher<S: StreamSpec>: Send + Sync + Sized
49where
50 S::Row: Sync,
51{
52 type Error: std::error::Error + From<PublicationError> + Send + Sync + 'static;
53
54 fn publish_on(&self, context: &ObserverContext<'_, Self>, row: &S::Row) -> impl Future<Output = Result<(), Self::Error>> + Send;
55}
56
57pub trait ObserverCaller<C>: Send + Sync + Sized {
58 type Request: Send;
59 type Response: Send;
60 type Error: std::error::Error + Send + Sync + 'static;
61
62 fn call(
63 &self, context: &ObserverContext<'_, Self>, request: Self::Request,
64 ) -> impl Future<Output = Result<Self::Response, Self::Error>> + Send;
65}
66
67impl ObserverHost for Supervisor {
70 fn bind(_: Arc<harmos_sdk::ExecutionContext>, supervisor: Supervisor) -> Self {
71 supervisor
72 }
73}
74
75impl<C> ObserverCaller<C> for Supervisor
76where
77 C: harmos_domain::operations::OperationDeclaration,
78{
79 type Request = C::Request;
80 type Response = C::Response;
81 type Error = CallError;
82
83 async fn call(&self, _: &ObserverContext<'_, Self>, request: Self::Request) -> Result<Self::Response, Self::Error> {
84 let bytes = self.child_call::<C>(&request, false).await?;
85 C::Response::decode(bytes.as_slice()).map_err(|error| CallError::Malformed {
86 method: C::ID,
87 message: error.to_string(),
88 })
89 }
90}
91
92