openapi_context/drop_context.rs
1//! Hyper service that adds a context to an incoming request and passes it on
2//! to a wrapped service.
3
4use crate::ContextualPayload;
5use hyper::{Body, Request};
6use std::marker::PhantomData;
7use std::task::{Context, Poll};
8
9/// Middleware wrapper service that drops the context from the incoming request
10/// and passes the plain `hyper::Request` to the wrapped service.
11///
12/// This service can be used to to include services that take a plain `hyper::Request`
13/// in a `CompositeService` wrapped in an `AddContextService`.
14///
15/// Example Usage
16/// =============
17///
18/// In the following example `SwaggerService` implements `hyper::service::MakeService`
19/// with `Request = (hyper::Request, SomeContext)`, and `PlainService` implements it
20/// with `Request = hyper::Request`
21///
22/// ```ignore
23/// let swagger_service_one = SwaggerService::new();
24/// let swagger_service_two = SwaggerService::new();
25/// let plain_service = PlainService::new();
26///
27/// let mut composite_new_service = CompositeMakeService::new();
28/// composite_new_service.push(("/base/path/1", swagger_service_one));
29/// composite_new_service.push(("/base/path/2", swagger_service_two));
30/// composite_new_service.push(("/base/path/3", DropContextMakeService::new(plain_service)));
31/// ```
32#[derive(Debug)]
33pub struct DropContextMakeService<C> {
34 phantom: PhantomData<C>,
35}
36
37impl<C> DropContextMakeService<C> {
38 /// Create a new DropContextMakeService struct wrapping a value
39 pub fn new() -> Self {
40 DropContextMakeService {
41 phantom: PhantomData,
42 }
43 }
44}
45
46impl<T, C> hyper::service::Service<T> for DropContextMakeService<C> {
47 type Response = DropContextService<T, C>;
48 type Error = std::io::Error;
49 type Future = futures::future::Ready<Result<Self::Response, Self::Error>>;
50
51 fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<std::result::Result<(), Self::Error>> {
52 Ok(()).into()
53 }
54
55 fn call(&mut self, inner: T) -> Self::Future {
56 futures::future::ok(DropContextService::new(inner))
57 }
58}
59
60/// Swagger Middleware that wraps a `hyper::service::Service`, and drops any contextual information
61/// on the request. Services will normally want to use `DropContextMakeService`, which will create
62/// a `DropContextService` to handle each connection.
63#[derive(Debug)]
64pub struct DropContextService<T, C> {
65 inner: T,
66 marker: PhantomData<C>,
67}
68
69impl<T, C> DropContextService<T, C> {
70 /// Create a new AddContextService struct wrapping a value
71 pub fn new(inner: T) -> Self {
72 DropContextService {
73 inner,
74 marker: PhantomData,
75 }
76 }
77}
78
79impl<T, C> hyper::service::Service<ContextualPayload<C>> for DropContextService<T, C>
80 where
81 C: Send + Sync + 'static,
82 T: hyper::service::Service<Request<Body>>,
83{
84 type Response = T::Response;
85 type Error = T::Error;
86 type Future = T::Future;
87
88 fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
89 Ok(()).into()
90 }
91
92 fn call(&mut self, req: ContextualPayload<C>) -> Self::Future {
93 self.inner.call(req.inner)
94 }
95}