tower_http/follow_redirect/policy/
redirect_fn.rs

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