tower_http/follow_redirect/policy/
clone_body_fn.rs

1use super::{Action, Attempt, Policy};
2use std::fmt;
3
4/// A redirection [`Policy`] created from a closure.
5///
6/// See [`clone_body_fn`] for more details.
7#[derive(Clone, Copy)]
8pub struct CloneBodyFn<F> {
9    f: F,
10}
11
12impl<F> fmt::Debug for CloneBodyFn<F> {
13    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14        f.debug_struct("CloneBodyFn")
15            .field("f", &std::any::type_name::<F>())
16            .finish()
17    }
18}
19
20impl<F, B, E> Policy<B, E> for CloneBodyFn<F>
21where
22    F: Fn(&B) -> Option<B>,
23{
24    fn redirect(&mut self, _: &Attempt<'_>) -> Result<Action, E> {
25        Ok(Action::Follow)
26    }
27
28    fn clone_body(&self, body: &B) -> Option<B> {
29        (self.f)(body)
30    }
31}
32
33/// Create a new redirection [`Policy`] from a closure `F: Fn(&B) -> Option<B>`.
34///
35/// [`clone_body`][Policy::clone_body] method of the returned `Policy` delegates to the wrapped
36/// closure and [`redirect`][Policy::redirect] method always returns [`Action::Follow`].
37pub fn clone_body_fn<F, B>(f: F) -> CloneBodyFn<F>
38where
39    F: Fn(&B) -> Option<B>,
40{
41    CloneBodyFn { f }
42}