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
97
98
use std::sync::Arc;
use super::{AndThen, Before, Map, MapErr, MapOk};
use crate::{Middleware, Request, Response, Result};
#[async_trait::async_trait]
pub trait Endpoint: Send + Sync + 'static {
async fn call(&self, req: Request) -> Result<Response>;
}
#[async_trait::async_trait]
impl<T: Endpoint + ?Sized> Endpoint for Box<T> {
async fn call(&self, req: Request) -> Result<Response> {
self.as_ref().call(req).await
}
}
#[async_trait::async_trait]
impl<T: Endpoint + ?Sized> Endpoint for Arc<T> {
async fn call(&self, req: Request) -> Result<Response> {
self.as_ref().call(req).await
}
}
pub trait EndpointExt: Endpoint {
fn with<T>(self, middleware: T) -> T::Output
where
T: Middleware<Self>,
Self: Sized,
{
middleware.transform(self)
}
fn before<F>(self, f: F) -> Before<Self, F>
where
F: Fn(Request) -> Result<Request>,
Self: Sized,
{
Before::new(self, f)
}
fn map<F>(self, f: F) -> Map<Self, F>
where
F: Fn(Result<Response>) -> Result<Response>,
Self: Sized,
{
Map::new(self, f)
}
fn and_then<F>(self, f: F) -> AndThen<Self, F>
where
F: Fn(Response) -> Result<Response>,
Self: Sized,
{
AndThen::new(self, f)
}
fn map_ok<F>(self, f: F) -> MapOk<Self, F>
where
F: Fn(Response) -> Response,
Self: Sized,
{
MapOk::new(self, f)
}
fn map_err<F>(self, f: F) -> MapErr<Self, F>
where
F: Fn(Response) -> Response,
Self: Sized,
{
MapErr::new(self, f)
}
}
impl<T: Endpoint> EndpointExt for T {}