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
use crate::{Connection, StratumRequest};
use async_std::{future::Future, sync::Arc};
use async_trait::async_trait;

pub(crate) type DynEndpoint<State, CState> = dyn Endpoint<State, CState>;

#[async_trait]
pub trait Endpoint<State: Clone + Send + Sync + 'static, CState: Clone + Send + Sync + 'static>:
    Send + Sync + 'static
{
    async fn call(
        &self,
        req: StratumRequest<State>,
        connection: Arc<Connection<CState>>,
    ) -> serde_json::Value;
}

#[async_trait]
impl<State, CState, F, Fut, Res, E> Endpoint<State, CState> for F
where
    State: Clone + Send + Sync + 'static,
    CState: Clone + Send + Sync + 'static,
    F: Send + Sync + 'static + Fn(StratumRequest<State>, Arc<Connection<CState>>) -> Fut,
    Fut: Future<Output = std::result::Result<Res, E>> + Send + 'static,
    E: std::error::Error + 'static + std::marker::Send,
    Res: Into<serde_json::Value> + 'static + std::marker::Send,
{
    async fn call(
        &self,
        req: StratumRequest<State>,
        connection: Arc<Connection<CState>>,
    ) -> serde_json::Value {
        let fut = (self)(req, connection.clone());

        match fut.await {
            Ok(res) => res.into(),
            Err(_e) => {
                connection.disconnect().await;
                serde_json::Value::Null
            }
        }
    }
}