harmos_sdk/operations/
surface.rs1use std::{any::Any, future::Future, sync::Arc};
23
24use super::instance::Instance;
28use super::{CallContext, ContextError, ContextValue, OperationDeclaration, Response};
29use crate::{Message, StreamSpec};
30
31#[derive(Clone)]
41pub struct RequestClaims {
42 owner: Arc<dyn Any + Send + Sync>,
43}
44
45pub trait Host: Send + Sync + Sized {
51 type Error: std::error::Error + Send + Sync + 'static;
52
53 type Instance<O: OperationDeclaration>: Instance<O, Error = Self::Error>;
54
55 fn context(&self) -> &CallContext;
56
57 fn with_value<T: ContextValue>(&self, value: T) -> Result<Self, ContextError>;
58
59 fn submit<O: OperationDeclaration>(&self, request: O::Request) -> impl Future<Output = Result<Self::Instance<O>, Self::Error>> + Send;
60
61 fn submit_owned<O: OperationDeclaration>(
64 &self, request: O::Request, claims: RequestClaims,
65 ) -> impl Future<Output = Result<Self::Instance<O>, Self::Error>> + Send;
66
67 fn call_owned<O: OperationDeclaration>(
68 &self, request: O::Request, claims: RequestClaims,
69 ) -> impl Future<Output = Result<Response<O::Response>, Self::Error>> + Send {
70 async move { self.submit_owned::<O>(request, claims).await?.join().await }
71 }
72
73 fn call<O: OperationDeclaration>(&self, request: O::Request)
74 -> impl Future<Output = Result<Response<O::Response>, Self::Error>> + Send;
75
76 fn publish_on<S: StreamSpec>(&self, row: &S::Row) -> impl Future<Output = Result<(), Self::Error>> + Send
78 where
79 S::Row: Message;
80
81 fn publish<S: StreamSpec<Row = S> + Message>(&self, row: &S) -> impl Future<Output = Result<(), Self::Error>> + Send {
82 self.publish_on::<S>(row)
83 }
84}
85
86impl RequestClaims {
89 pub fn retain(owner: Arc<dyn Any + Send + Sync>) -> Self {
90 Self { owner }
91 }
92
93 pub fn owner<T: Any + Send + Sync>(&self) -> Option<&T> {
94 self.owner.downcast_ref()
95 }
96}
97
98