workflow_rpc/client/interface/
mod.rs

1pub mod notification;
2use crate::imports::*;
3pub use notification::*;
4
5/// Collection of server-side notification handlers
6pub struct Interface<Ops>
7where
8    Ops: OpsT,
9{
10    notifications: AHashMap<Ops, Box<dyn NotificationTrait>>,
11}
12
13impl<Ops> Default for Interface<Ops>
14where
15    Ops: OpsT,
16{
17    fn default() -> Self {
18        Self::new()
19    }
20}
21
22impl<Ops> Interface<Ops>
23where
24    Ops: OpsT,
25{
26    pub fn new() -> Interface<Ops> {
27        Interface {
28            notifications: AHashMap::new(),
29        }
30    }
31
32    pub fn notification<Msg>(&mut self, op: Ops, method: Notification<Msg>)
33    where
34        Ops: OpsT,
35        Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
36    {
37        let method: Box<dyn NotificationTrait> = Box::new(method);
38        if self.notifications.insert(op.clone(), method).is_some() {
39            panic!("RPC notification {op:?} is declared multiple times")
40        }
41    }
42
43    pub async fn call_notification_with_borsh(&self, op: &Ops, payload: &[u8]) -> ServerResult<()> {
44        if let Some(notification) = self.notifications.get(op) {
45            notification.call_with_borsh(payload).await
46        } else {
47            Err(ServerError::NotFound)
48        }
49    }
50
51    pub async fn call_notification_with_serde_json(
52        &self,
53        op: &Ops,
54        payload: Value,
55    ) -> ServerResult<()> {
56        if let Some(notification) = self.notifications.get(op) {
57            notification.call_with_serde_json(payload).await
58        } else {
59            Err(ServerError::NotFound)
60        }
61    }
62}
63
64impl<Ops> From<Interface<Ops>> for Option<Arc<Interface<Ops>>>
65where
66    Ops: OpsT,
67{
68    fn from(interface: Interface<Ops>) -> Self {
69        Some(Arc::new(interface))
70    }
71}