11use super :: scope:: Scope ;
2- use crate :: {
3- http:: IntoRequest ,
4- valar:: { Valar , map:: Map , scope:: ScopeLayer } ,
5- } ;
6- use tower:: Layer ;
2+ use crate :: valar:: { Valar , scope:: ScopeLayer } ;
3+ use tower_layer:: Layer ;
74
85#[ derive( Debug , Clone , new) ]
96pub struct Builder < S > ( S ) ;
@@ -12,6 +9,10 @@ impl<S> Builder<S>
129where
1310 S : Clone ,
1411{
12+ pub fn into_inner ( self ) -> S {
13+ self . 0
14+ }
15+
1516 pub fn layer < L > ( self , layer : L ) -> Builder < S :: Output >
1617 where
1718 S : ApplyLayer < L > ,
@@ -188,10 +189,15 @@ impl_apply_layer!(
188189
189190#[ cfg( test) ]
190191mod tests {
191- use std:: convert:: Infallible ;
192192
193193 use super :: * ;
194- use crate :: { http:: Request , util:: NoopService } ;
194+ use crate :: BoxFuture ;
195+ use crate :: http:: { IntoRequest , Response } ;
196+ use crate :: valar:: builder;
197+ use crate :: valar:: {
198+ credential:: Credential ,
199+ handler:: { Handler , State } ,
200+ } ;
195201
196202 #[ derive( Debug , Clone ) ]
197203 struct FooLayer ;
@@ -201,21 +207,69 @@ mod tests {
201207 inner : S ,
202208 }
203209
204- impl < S > tower:: Service < Request > for FooService < S > {
205- type Response = Infallible ;
206- type Error = Infallible ;
207- type Future = std:: pin:: Pin < Box < dyn Future < Output = Result < Self :: Response , Self :: Error > > + Send + ' static > > ;
210+ impl < S , Request > Handler < Request > for FooService < S >
211+ where
212+ S : Handler < Request > ,
213+ Request : IntoRequest ,
214+ {
215+ type Response = S :: Response ;
216+ type Error = S :: Error ;
217+ type Future = S :: Future ;
218+
219+ fn authenticate ( & self , request : Request , state : & dyn State ) -> Self :: Future {
220+ todo ! ( )
221+ }
222+
223+ fn forbid ( & self , request : Request , state : & dyn State ) -> Self :: Future {
224+ todo ! ( )
225+ }
226+
227+ fn challenge ( & self , request : Request , state : & dyn State ) -> Self :: Future {
228+ todo ! ( )
229+ }
230+
231+ fn sign_out ( & self , request : Request , state : & dyn State ) -> Self :: Future {
232+ todo ! ( )
233+ }
234+
235+ fn sign_in ( & self , request : Request , state : & dyn State , credential : Credential ) -> Self :: Future {
236+ todo ! ( )
237+ }
238+ }
239+
240+ #[ derive( Debug , Clone ) ]
241+ struct NoopHandler ;
242+
243+ impl < Request > Handler < Request > for NoopHandler
244+ where
245+ Request : IntoRequest ,
246+ {
247+ type Response = Response ;
248+ type Error = Response ;
249+ type Future = BoxFuture < ' static , Result < Self :: Response , Self :: Error > > ;
250+
251+ fn authenticate ( & self , request : Request , state : & dyn State ) -> Self :: Future {
252+ todo ! ( )
253+ }
254+
255+ fn forbid ( & self , request : Request , state : & dyn State ) -> Self :: Future {
256+ todo ! ( )
257+ }
208258
209- fn poll_ready ( & mut self , cx : & mut std :: task :: Context < ' _ > ) -> std :: task :: Poll < Result < ( ) , Self :: Error > > {
259+ fn challenge ( & self , request : Request , state : & dyn State ) -> Self :: Future {
210260 todo ! ( )
211261 }
212262
213- fn call ( & mut self , req : Request ) -> Self :: Future {
263+ fn sign_out ( & self , request : Request , state : & dyn State ) -> Self :: Future {
264+ todo ! ( )
265+ }
266+
267+ fn sign_in ( & self , request : Request , state : & dyn State , credential : Credential ) -> Self :: Future {
214268 todo ! ( )
215269 }
216270 }
217271
218- impl < S > tower :: Layer < S > for FooLayer
272+ impl < S > tower_layer :: Layer < S > for FooLayer
219273 where
220274 S : Clone ,
221275 {
@@ -228,10 +282,11 @@ mod tests {
228282
229283 #[ test]
230284 fn test_builder ( ) {
231- let builder = Builder :: new ( ( NoopService , ) )
232- . handler ( NoopService )
285+ let ( _service_1, _) = builder:: Builder :: new ( ( NoopHandler , ) )
233286 . layer ( FooLayer )
234- . handler ( NoopService )
235- . scope_layer ( Scope :: SignIn , FooLayer ) ;
287+ . handler ( NoopHandler )
288+ . into_inner ( ) ;
289+
290+ // let _ = service_1.authenticate(request, state);
236291 }
237292}
0 commit comments