actix_error_mapper_middleware/
service.rs

1use super::MapperMiddleware;
2use actix_web::{
3    body::MessageBody,
4    dev::{Service, ServiceRequest, ServiceResponse, Transform},
5    Error, ResponseError,
6};
7use futures_util::future;
8use std::{marker::PhantomData, rc::Rc};
9
10/// # MapperSerivce
11/// this service stores the Custom errror Type in a field using ```std::marker::PhantomData``` 
12pub struct MapperService<E> {
13    _error: PhantomData<E>,
14}
15
16impl<E> MapperService<E> {
17    pub fn new() -> MapperService<E> {
18        MapperService {
19            _error: PhantomData,
20        }
21    }
22}
23
24impl<S, B, E> Transform<S, ServiceRequest> for MapperService<E>
25where
26    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
27    S::Future: 'static,
28    B: MessageBody,
29    E: ResponseError + From<Error> + 'static,
30{
31    type Response = <MapperMiddleware<S, E> as Service<ServiceRequest>>::Response;
32    type Error = Error;
33    type Transform = MapperMiddleware<S, E>;
34    type InitError = ();
35    type Future = future::Ready<Result<Self::Transform, Self::InitError>>;
36
37    fn new_transform(&self, service: S) -> Self::Future {
38        future::ok(MapperMiddleware {
39            service: Rc::new(service),
40            _error: self._error,
41        })
42    }
43}