boluo_core/service/
map_err.rs1use super::Service;
2
3#[derive(Clone, Copy)]
7pub struct MapErr<S, F> {
8 service: S,
9 f: F,
10}
11
12impl<S, F> MapErr<S, F> {
13 pub fn new(service: S, f: F) -> Self {
15 Self { service, f }
16 }
17}
18
19impl<S, F, Req, Err> Service<Req> for MapErr<S, F>
20where
21 S: Service<Req>,
22 F: Fn(S::Error) -> Err + Send + Sync,
23{
24 type Response = S::Response;
25 type Error = Err;
26
27 fn call(&self, req: Req) -> impl Future<Output = Result<Self::Response, Self::Error>> + Send {
28 let fut = self.service.call(req);
29 async move { fut.await.map_err(|err| (self.f)(err)) }
30 }
31}
32
33impl<S, F> std::fmt::Debug for MapErr<S, F>
34where
35 S: std::fmt::Debug,
36{
37 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38 f.debug_struct("MapErr")
39 .field("service", &self.service)
40 .field("f", &std::any::type_name::<F>())
41 .finish()
42 }
43}