gotham/router/response/
extender.rs

1//! Defines functionality for extending a Response.
2
3use crate::state::{request_id, State};
4use hyper::body::HttpBody;
5use hyper::{Body, Response};
6use log::trace;
7use std::panic::RefUnwindSafe;
8
9/// Extend the `Response` based on current `State` and `Response` data.
10pub trait StaticResponseExtender: RefUnwindSafe {
11    /// The type of the response body. Almost always `hyper::Body`.
12    type ResBody: HttpBody;
13
14    /// Extend the response.
15    fn extend(state: &mut State, response: &mut Response<Self::ResBody>);
16}
17
18/// Allow complex types to extend the `Response` based on current `State` and `Response` data.
19pub trait ResponseExtender<B>: RefUnwindSafe {
20    /// Extend the Response
21    fn extend(&self, state: &mut State, response: &mut Response<B>);
22}
23
24impl<F, B> ResponseExtender<B> for F
25where
26    F: Fn(&mut State, &mut Response<B>) + Send + Sync + RefUnwindSafe,
27{
28    fn extend(&self, state: &mut State, res: &mut Response<B>) {
29        trace!(
30            "[{}] running closure based response extender",
31            request_id(state)
32        );
33        self(state, res);
34    }
35}
36
37/// An extender that does not alter the response.
38///
39/// This is likely to only be useful in documentation or example code.
40pub struct NoopResponseExtender;
41
42impl StaticResponseExtender for NoopResponseExtender {
43    type ResBody = Body;
44
45    fn extend(state: &mut State, _res: &mut Response<Body>) {
46        trace!(
47            "[{}] NoopResponseExtender invoked, does not make any changes to Response",
48            request_id(state)
49        );
50        trace!("[{}] no response body, no change made", request_id(state));
51    }
52}
53
54impl ResponseExtender<Body> for NoopResponseExtender {
55    fn extend(&self, state: &mut State, _res: &mut Response<Body>) {
56        trace!(
57            "[{}] NoopResponseExtender invoked on instance, does not make any changes to Response",
58            request_id(state)
59        );
60        trace!("[{}] no response body, no change made", request_id(state));
61    }
62}