gotham/router/response/
extender.rs1use crate::state::{request_id, State};
4use hyper::body::HttpBody;
5use hyper::{Body, Response};
6use log::trace;
7use std::panic::RefUnwindSafe;
8
9pub trait StaticResponseExtender: RefUnwindSafe {
11 type ResBody: HttpBody;
13
14 fn extend(state: &mut State, response: &mut Response<Self::ResBody>);
16}
17
18pub trait ResponseExtender<B>: RefUnwindSafe {
20 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
37pub 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}