actix_state_guards/
transform.rs1use crate::middleware::StateGuardMiddleware;
2use crate::Guard as GuardTrait;
3
4use std::future::{ready, Ready};
5use std::marker::PhantomData;
6use std::rc::Rc;
7
8use actix_web::body::MessageBody;
9use actix_web::dev::{Service as ServiceTrait, ServiceRequest, ServiceResponse, Transform};
10use actix_web::{Error as ActixWebError, FromRequest, ResponseError};
11
12pub struct StateGuard<Guard, Args, Error> {
13 guard: Rc<Guard>,
14 args_marker: PhantomData<Args>,
15 error_marker: PhantomData<Error>,
16}
17
18impl<Guard, Args, Error> StateGuard<Guard, Args, Error> {
19 pub fn new(guard: Guard) -> Self {
20 Self {
21 guard: Rc::new(guard),
22 args_marker: PhantomData,
23 error_marker: PhantomData,
24 }
25 }
26}
27
28impl<Service, Body, Guard, Args, Error> Transform<Service, ServiceRequest>
29 for StateGuard<Guard, Args, Error>
30where
31 Service: ServiceTrait<ServiceRequest, Response = ServiceResponse<Body>, Error = ActixWebError>
32 + 'static,
33 Body: MessageBody,
34 Guard: GuardTrait<Args, Error>,
35 Error: ResponseError + 'static,
36 Args: FromRequest,
37{
38 type Response = <StateGuardMiddleware<Service, Guard, Args, Error> as ServiceTrait<
39 ServiceRequest,
40 >>::Response;
41
42 type Error =
43 <StateGuardMiddleware<Service, Guard, Args, Error> as ServiceTrait<ServiceRequest>>::Error;
44
45 type Transform = StateGuardMiddleware<Service, Guard, Args, Error>;
46
47 type InitError = ();
48
49 type Future = Ready<Result<Self::Transform, Self::InitError>>;
50
51 fn new_transform(&self, service: Service) -> Self::Future {
52 ready(Ok(StateGuardMiddleware::new(
53 Rc::clone(&self.guard),
54 service,
55 )))
56 }
57}