1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//! Module containing RPC [`Notification`] closure wrappers
use crate::imports::*;

/// Base trait representing an RPC notification, used to retain
/// notification structures in an [`Interface`](super::Interface)
/// map without generics.
#[async_trait]
pub(crate) trait NotificationTrait<ServerContext, ConnectionContext>:
    Send + Sync + 'static
{
    async fn call_with_borsh(
        &self,
        server_ctx: ServerContext,
        connection_ctx: ConnectionContext,
        data: &[u8],
    ) -> ServerResult<()>;
    async fn call_with_serde_json(
        &self,
        server_ctx: ServerContext,
        connection_ctx: ConnectionContext,
        value: Value,
    ) -> ServerResult<()>;
}

/// Notification closure type
pub type NotificationFn<ServerContext, ConnectionContext, Msg> = Arc<
    Box<
        dyn Send
            + Sync
            + Fn(ServerContext, ConnectionContext, Msg) -> NotificationFnReturn<()>
            + 'static,
    >,
>;

/// Notification closure return type
pub type NotificationFnReturn<T> =
    Pin<Box<(dyn Send + 'static + Future<Output = ServerResult<T>>)>>;

/// RPC notification wrapper. Contains the notification closure function.

pub struct Notification<ServerContext, ConnectionContext, Msg>
where
    ServerContext: Send + Sync + 'static,
    Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
{
    method: NotificationFn<ServerContext, ConnectionContext, Msg>,
}

impl<ServerContext, ConnectionContext, Msg> Notification<ServerContext, ConnectionContext, Msg>
where
    ServerContext: Send + Sync + 'static,
    Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
{
    pub fn new<FN>(method_fn: FN) -> Notification<ServerContext, ConnectionContext, Msg>
    where
        FN: Send
            + Sync
            + Fn(ServerContext, ConnectionContext, Msg) -> NotificationFnReturn<()>
            + 'static,
    {
        Notification {
            method: Arc::new(Box::new(method_fn)),
        }
    }
}

#[async_trait]
impl<ServerContext, ConnectionContext, Msg> NotificationTrait<ServerContext, ConnectionContext>
    for Notification<ServerContext, ConnectionContext, Msg>
where
    ConnectionContext: Clone + Send + Sync + 'static,
    ServerContext: Send + Sync + 'static,
    Msg: BorshDeserialize + DeserializeOwned + Send + Sync + 'static,
{
    async fn call_with_borsh(
        &self,
        server_ctx: ServerContext,
        connection_ctx: ConnectionContext,
        data: &[u8],
    ) -> ServerResult<()> {
        let req = Msg::try_from_slice(data)
            .map_err(|err| ServerError::NotificationDeserialize(err.to_string()))?;
        (self.method)(server_ctx, connection_ctx, req).await
    }

    async fn call_with_serde_json(
        &self,
        server_ctx: ServerContext,
        connection_ctx: ConnectionContext,
        value: Value,
    ) -> ServerResult<()> {
        let req: Msg = serde_json::from_value(value)
            .map_err(|err| ServerError::NotificationDeserialize(err.to_string()))?;
        (self.method)(server_ctx, connection_ctx, req).await
    }
}