1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::marker::PhantomData;

use futures::{Async, Future, Poll};

use crate::and_then::AndThen;
use crate::{IntoNewService, NewService};

/// `ApplyNewService` new service combinator
pub struct ApplyConfig<F, A, B, C1, C2> {
    a: A,
    b: B,
    f: F,
    r: PhantomData<(C1, C2)>,
}

impl<F, A, B, C1, C2> ApplyConfig<F, A, B, C1, C2>
where
    A: NewService<C1>,
    B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
    F: Fn(&C1) -> C2,
{
    /// Create new `ApplyNewService` new service instance
    pub fn new<A1: IntoNewService<A, C1>, B1: IntoNewService<B, C2>>(
        a: A1,
        b: B1,
        f: F,
    ) -> Self {
        Self {
            f,
            a: a.into_new_service(),
            b: b.into_new_service(),
            r: PhantomData,
        }
    }
}

impl<F, A, B, C1, C2> Clone for ApplyConfig<F, A, B, C1, C2>
where
    A: Clone,
    B: Clone,
    F: Clone,
{
    fn clone(&self) -> Self {
        Self {
            a: self.a.clone(),
            b: self.b.clone(),
            f: self.f.clone(),
            r: PhantomData,
        }
    }
}

impl<F, A, B, C1, C2> NewService<C1> for ApplyConfig<F, A, B, C1, C2>
where
    A: NewService<C1>,
    B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
    F: Fn(&C1) -> C2,
{
    type Request = A::Request;
    type Response = B::Response;
    type Error = A::Error;
    type Service = AndThen<A::Service, B::Service>;

    type InitError = A::InitError;
    type Future = ApplyConfigResponse<A, B, C1, C2>;

    fn new_service(&self, cfg: &C1) -> Self::Future {
        let cfg2 = (self.f)(cfg);

        ApplyConfigResponse {
            a: None,
            b: None,
            fut_a: self.a.new_service(cfg),
            fut_b: self.b.new_service(&cfg2),
        }
    }
}

pub struct ApplyConfigResponse<A, B, C1, C2>
where
    A: NewService<C1>,
    B: NewService<C2>,
{
    fut_b: B::Future,
    fut_a: A::Future,
    a: Option<A::Service>,
    b: Option<B::Service>,
}

impl<A, B, C1, C2> Future for ApplyConfigResponse<A, B, C1, C2>
where
    A: NewService<C1>,
    B: NewService<C2, Request = A::Response, Error = A::Error, InitError = A::InitError>,
{
    type Item = AndThen<A::Service, B::Service>;
    type Error = A::InitError;

    fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
        if self.a.is_none() {
            if let Async::Ready(service) = self.fut_a.poll()? {
                self.a = Some(service);
            }
        }

        if self.b.is_none() {
            if let Async::Ready(service) = self.fut_b.poll()? {
                self.b = Some(service);
            }
        }

        if self.a.is_some() && self.b.is_some() {
            Ok(Async::Ready(AndThen::new(
                self.a.take().unwrap(),
                self.b.take().unwrap(),
            )))
        } else {
            Ok(Async::NotReady)
        }
    }
}

#[cfg(test)]
mod tests {
    use futures::future::{ok, FutureResult};
    use futures::{Async, Future, Poll};

    use crate::{fn_cfg_factory, NewService, Service};

    #[derive(Clone)]
    struct Srv;
    impl Service for Srv {
        type Request = ();
        type Response = ();
        type Error = ();
        type Future = FutureResult<(), ()>;

        fn poll_ready(&mut self) -> Poll<(), Self::Error> {
            Ok(Async::Ready(()))
        }

        fn call(&mut self, _: ()) -> Self::Future {
            ok(())
        }
    }

    #[test]
    fn test_new_service() {
        let new_srv = fn_cfg_factory(|_: &usize| Ok::<_, ()>(Srv)).apply_cfg(
            fn_cfg_factory(|s: &String| {
                assert_eq!(s, "test");
                Ok::<_, ()>(Srv)
            }),
            |cfg: &usize| {
                assert_eq!(*cfg, 1);
                "test".to_string()
            },
        );

        if let Async::Ready(mut srv) = new_srv.new_service(&1).poll().unwrap() {
            assert!(srv.poll_ready().is_ok());
        }
    }
}