@@ -3,6 +3,8 @@ use crate::route::{Route, Segment, RouteUri};
3
3
4
4
use crate :: http:: MediaType ;
5
5
6
+ use super :: unique_property;
7
+
6
8
pub trait Collide < T = Self > {
7
9
fn collides_with ( & self , other : & T ) -> bool ;
8
10
}
@@ -50,11 +52,11 @@ impl Route {
50
52
/// assert!(a.collides_with(&b));
51
53
///
52
54
/// // Two routes with the same method, rank, URI, and overlapping formats.
53
- /// let mut a = Route::new(Method::Post, "/", handler);
54
- /// a.format = Some(MediaType::new("*", "custom"));
55
- /// let mut b = Route::new(Method::Post, "/", handler);
56
- /// b.format = Some(MediaType::new("text", "*"));
57
- /// assert!(a.collides_with(&b));
55
+ /// // let mut a = Route::new(Method::Post, "/", handler);
56
+ /// // a.format = Some(MediaType::new("*", "custom"));
57
+ /// // let mut b = Route::new(Method::Post, "/", handler);
58
+ /// // b.format = Some(MediaType::new("text", "*"));
59
+ /// // assert!(a.collides_with(&b));
58
60
///
59
61
/// // Two routes with different ranks don't collide.
60
62
/// let a = Route::ranked(1, Method::Get, "/", handler);
@@ -72,25 +74,26 @@ impl Route {
72
74
/// assert!(!a.collides_with(&b));
73
75
///
74
76
/// // Two payload-supporting routes with non-overlapping formats.
75
- /// let mut a = Route::new(Method::Post, "/", handler);
76
- /// a.format = Some(MediaType::HTML);
77
- /// let mut b = Route::new(Method::Post, "/", handler);
78
- /// b.format = Some(MediaType::JSON);
79
- /// assert!(!a.collides_with(&b));
77
+ /// // let mut a = Route::new(Method::Post, "/", handler);
78
+ /// // a.format = Some(MediaType::HTML);
79
+ /// // let mut b = Route::new(Method::Post, "/", handler);
80
+ /// // b.format = Some(MediaType::JSON);
81
+ /// // assert!(!a.collides_with(&b));
80
82
///
81
83
/// // Two non payload-supporting routes with non-overlapping formats
82
84
/// // collide. A request with `Accept: */*` matches both.
83
- /// let mut a = Route::new(Method::Get, "/", handler);
84
- /// a.format = Some(MediaType::HTML);
85
- /// let mut b = Route::new(Method::Get, "/", handler);
86
- /// b.format = Some(MediaType::JSON);
87
- /// assert!(a.collides_with(&b));
85
+ /// // let mut a = Route::new(Method::Get, "/", handler);
86
+ /// // a.format = Some(MediaType::HTML);
87
+ /// // let mut b = Route::new(Method::Get, "/", handler);
88
+ /// // b.format = Some(MediaType::JSON);
89
+ /// // assert!(a.collides_with(&b));
88
90
/// ```
89
91
pub fn collides_with ( & self , other : & Route ) -> bool {
90
92
self . method == other. method
91
93
&& self . rank == other. rank
92
94
&& self . uri . collides_with ( & other. uri )
93
- && formats_collide ( self , other)
95
+ // && formats_collide(self, other)
96
+ && unique_property:: collides ( & self , & other)
94
97
}
95
98
}
96
99
@@ -190,23 +193,6 @@ impl Collide for MediaType {
190
193
}
191
194
}
192
195
193
- fn formats_collide ( route : & Route , other : & Route ) -> bool {
194
- match ( route. method . allows_request_body ( ) , other. method . allows_request_body ( ) ) {
195
- // Payload supporting methods match against `Content-Type` which must be
196
- // fully specified, so the request cannot contain a format that matches
197
- // more than one route format as long as those formats don't collide.
198
- ( Some ( true ) , Some ( true ) ) => match ( route. format . as_ref ( ) , other. format . as_ref ( ) ) {
199
- ( Some ( a) , Some ( b) ) => a. collides_with ( b) ,
200
- // A route without a `format` accepts all `Content-Type`s.
201
- _ => true
202
- } ,
203
- // When a request method may not support a payload, the `Accept` header
204
- // is considered during matching. The header can always be `*/*`, which
205
- // would match any format. Thus two such routes would always collide.
206
- _ => true ,
207
- }
208
- }
209
-
210
196
#[ cfg( test) ]
211
197
mod tests {
212
198
use std:: str:: FromStr ;
@@ -420,12 +406,12 @@ mod tests {
420
406
{
421
407
let mut route_a = Route :: new ( m, "/" , dummy_handler) ;
422
408
if let Some ( mt_str) = mt1. into ( ) {
423
- route_a. format = Some ( mt_str. parse :: < MediaType > ( ) . unwrap ( ) ) ;
409
+ route_a. add_unique_prop ( mt_str. parse :: < MediaType > ( ) . unwrap ( ) ) ;
424
410
}
425
411
426
412
let mut route_b = Route :: new ( m, "/" , dummy_handler) ;
427
413
if let Some ( mt_str) = mt2. into ( ) {
428
- route_b. format = Some ( mt_str. parse :: < MediaType > ( ) . unwrap ( ) ) ;
414
+ route_b. add_unique_prop ( mt_str. parse :: < MediaType > ( ) . unwrap ( ) ) ;
429
415
}
430
416
431
417
route_a. collides_with ( & route_b)
@@ -473,6 +459,15 @@ mod tests {
473
459
assert ! ( !r_mt_mt_collide( Post , "other/html" , "text/html" ) ) ;
474
460
}
475
461
462
+ #[ test]
463
+ fn check_prop_collider ( ) {
464
+ // let a = "application/*".parse::<MediaType>().unwrap();
465
+ // let b = "text/*".parse::<MediaType>().unwrap();
466
+ // assert_eq!(a.collides_with(&b), true);
467
+ // assert_eq!(a.collides(&b), Some(true));
468
+ // assert!(unique_property::collides(&vec![Box::new(a)], &vec![Box::new(b)]));
469
+ }
470
+
476
471
fn catchers_collide < A , B > ( a : A , ap : & str , b : B , bp : & str ) -> bool
477
472
where A : Into < Option < u16 > > , B : Into < Option < u16 > >
478
473
{
0 commit comments