1use crate::service::Service;
2use std::future::Future;
3use std::marker::PhantomData;
4use crate::Layer;
5
6pub struct FnService<F, Request, R, Response, E>
7where
8 F: Fn(Request) -> R + Send + Sync + 'static,
9 R: Future<Output=Result<Response, E>>,
10{
11 f: F,
12 _t: PhantomData<fn(Request, R, Response) -> ()>,
13}
14
15impl<F, Request, R, Response, E> FnService<F, Request, R, Response, E>
16where
17 F: Fn(Request) -> R + Send + Sync + 'static,
18 R: Future<Output=Result<Response, E>>,
19{
20 pub fn new(f: F) -> Self {
21 Self {
22 f,
23 _t: PhantomData,
24 }
25 }
26}
27
28impl<F, Request, R, Response, E> Clone for FnService<F, Request, R, Response, E>
29where
30 F: Fn(Request) -> R + Send + Sync + 'static + Clone,
31 R: Future<Output=Result<Response, E>>,
32{
33 fn clone(&self) -> Self {
34 Self {
35 f: self.f.clone(),
36 _t: PhantomData,
37 }
38 }
39}
40
41impl<F, Request, R, Response, E> Service<Request> for FnService<F, Request, R, Response, E>
42where
43 F: Fn(Request) -> R + Send + Sync + 'static,
44 R: Future<Output=Result<Response, E>> + Send + 'static,
45 Response: Send + 'static,
46 E: Send + Sync + 'static,
47 Request: 'static,
48{
49 type Response = Response;
50 type Error = E;
51
52 fn call(
53 &self,
54 request: Request,
55 ) -> impl Future<Output=Result<Self::Response, Self::Error>> + Send + '_ {
56 (self.f)(request)
57 }
58}
59
60
61pub struct MapResponse<S, F> {
62 inner: S,
63 f: F,
64}
65
66
67impl<S, F, Request, R> Service<Request> for MapResponse<S, F>
68where
69 S: Service<Request> + 'static,
70 Request: Send + 'static,
71 F: FnOnce(Result<S::Response, S::Error>) -> R + Clone + Sync + Send + 'static,
72 R: Future<Output=Result<S::Response, S::Error>> + Send + 'static + Send,
73{
74 type Response = S::Response;
75 type Error = S::Error;
76
77 async fn call(
78 &self,
79 request: Request,
80 ) -> Result<Self::Response, Self::Error> {
81 let resp = self.inner.call(request).await;
82 self.f.clone()(resp).await
83 }
84}
85
86#[derive(Clone)]
88pub struct MapResponseLayer<F> {
89 f: F,
90}
91
92impl<F> MapResponseLayer<F> {
93 pub fn new(f: F) -> Self {
94 Self { f }
95 }
96}
97
98impl<S, F> Layer<S> for MapResponseLayer<F>
99where
100 F: Clone,
101 S: Send + Sync + 'static
102{
103 type Service = MapResponse<S, F>;
104
105 fn layer(self, inner: S) -> Self::Service {
106 MapResponse {
107 f: self.f,
108 inner,
109 }
110 }
111}
112
113pub struct MapRequest<S, F> {
115 inner: S,
116 f: F,
117}
118
119
120impl<S, F, Request, R> Service<Request> for MapRequest<S, F>
121where
122 S: Service<Request> + 'static,
123 Request: Send + 'static,
124 F: FnOnce(Request) -> R + Clone + Sync + Send + 'static,
125 R: Future<Output=Result<Request, S::Error>> + Send + 'static + Send,
126{
127 type Response = S::Response;
128 type Error = S::Error;
129
130 async fn call(
131 &self,
132 request: Request,
133 ) -> Result<Self::Response, Self::Error> {
134 let request = self.f.clone()(request).await?;
135 self.inner.call(request).await
136 }
137}
138
139
140#[derive(Clone)]
141pub struct MapRequestLayer<F> {
142 f: F,
143}
144
145impl<F> MapRequestLayer<F> {
146 pub fn new(f: F) -> Self {
147 Self { f }
148 }
149}
150
151impl<S, F> Layer<S> for MapRequestLayer<F>
152where
153 F: Clone,
154 S: Send + Sync + 'static
155{
156 type Service = MapRequest<S, F>;
157
158 fn layer(self, inner: S) -> Self::Service {
159 MapRequest {
160 f: self.f,
161 inner,
162 }
163 }
164}