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
use std::future::Future;
use std::sync::Arc;

use crate::router::router::Router;
use crate::request::request::Request;
use crate::response::response::Response;

pub(crate) type DynEndpoint = dyn Endpoint;

#[crate::async_trait]
pub trait Endpoint: Send + Sync + 'static {
    /// Invoke the endpoint within the given context
    async fn call(&self, req: Request) -> Response;
}

#[crate::async_trait]
impl<F: Send + Sync + 'static, Fut, Res> Endpoint for F
    where
        F: Fn(Request) -> Fut,
        Fut: Future<Output=Res> + Send + 'static,
        Res: Into<Response> + 'static,
{
    async fn call(&self, req: Request) -> Response {
        let resp = self(req).await;
        resp.into()
    }
}

pub(crate) struct RouterEndpoint {
    router: Arc<Router>,
}

impl RouterEndpoint {
    pub(crate) fn new(router: Arc<Router>) -> RouterEndpoint {
        RouterEndpoint { router }
    }
}

#[crate::async_trait]
impl Endpoint for RouterEndpoint {
    async fn call(&self, req: Request) -> Response {
        self.router.route(req).await
    }
}

impl std::fmt::Debug for RouterEndpoint {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "RouterEndpoint{{ router: {:?} }}", self.router)
    }
}