135
135
#![ forbid( unsafe_code) ]
136
136
#![ warn( rust_2018_idioms, unreachable_pub) ]
137
137
138
- use std:: { iter, marker:: PhantomData , slice, str:: from_utf8} ;
138
+ use std:: {
139
+ iter:: { Copied , FilterMap , Zip } ,
140
+ marker:: PhantomData ,
141
+ slice:: Iter ,
142
+ str:: from_utf8,
143
+ } ;
139
144
140
145
use smallvec:: SmallVec ;
141
146
@@ -145,6 +150,7 @@ mod parser;
145
150
pub use node:: { Node , NodeKind } ;
146
151
pub use parser:: { Kind , Parser , Piece , Position } ;
147
152
153
+ /// A path tree.
148
154
#[ derive( Debug ) ]
149
155
pub struct PathTree < T > {
150
156
id : usize ,
@@ -159,6 +165,7 @@ impl<T> Default for PathTree<T> {
159
165
}
160
166
161
167
impl < T > PathTree < T > {
168
+ /// Creates a new [PathTree].
162
169
pub fn new ( ) -> Self {
163
170
Self {
164
171
id : 0 ,
@@ -167,6 +174,7 @@ impl<T> PathTree<T> {
167
174
}
168
175
}
169
176
177
+ /// Inserts a part path-value to the tree and returns the id.
170
178
pub fn insert ( & mut self , path : & str , value : T ) -> usize {
171
179
let mut node = & mut self . node ;
172
180
@@ -202,6 +210,7 @@ impl<T> PathTree<T> {
202
210
}
203
211
}
204
212
213
+ /// Returns the [Path] by the given path.
205
214
pub fn find < ' b > ( & self , path : & ' b str ) -> Option < Path < ' _ , ' b , T > > {
206
215
let bytes = path. as_bytes ( ) ;
207
216
self . node . find ( bytes) . and_then ( |( id, ranges) | {
@@ -221,12 +230,13 @@ impl<T> PathTree<T> {
221
230
} )
222
231
}
223
232
233
+ /// Gets the route by id.
224
234
#[ inline]
225
235
pub fn get_route ( & self , index : usize ) -> Option < & ( T , Vec < Piece > ) > {
226
236
self . routes . get ( index)
227
237
}
228
238
229
- /// Generates URL
239
+ /// Generates URL with the params.
230
240
pub fn url_for ( & self , index : usize , params : & [ & str ] ) -> Option < String > {
231
241
self . get_route ( index) . map ( |( _, pieces) | {
232
242
let mut bytes = Vec :: new ( ) ;
@@ -248,6 +258,7 @@ impl<T> PathTree<T> {
248
258
}
249
259
}
250
260
261
+ /// Matched route path infomation.
251
262
#[ derive( Debug , PartialEq , Eq ) ]
252
263
pub struct Path < ' a , ' b , T > {
253
264
pub id : & ' a usize ,
@@ -257,53 +268,54 @@ pub struct Path<'a, 'b, T> {
257
268
}
258
269
259
270
impl < ' a , ' b , T > Path < ' a , ' b , T > {
271
+ /// Gets current path pattern.
260
272
pub fn pattern ( & self ) -> String {
261
273
let mut bytes = Vec :: new ( ) ;
262
274
263
- for piece in self . pieces {
264
- match piece {
265
- Piece :: String ( s) => {
266
- if s == b":" || s == b"+" || s == b"?" {
267
- bytes. push ( b'\\' ) ;
275
+ self . pieces . iter ( ) . for_each ( |piece| match piece {
276
+ Piece :: String ( s) => {
277
+ if s == b":" || s == b"+" || s == b"?" {
278
+ bytes. push ( b'\\' ) ;
279
+ }
280
+ bytes. extend_from_slice ( s) ;
281
+ }
282
+ Piece :: Parameter ( p, k) => match p {
283
+ Position :: Index ( _, _) => {
284
+ if * k == Kind :: OneOrMore {
285
+ bytes. push ( b'+' ) ;
286
+ } else if * k == Kind :: ZeroOrMore || * k == Kind :: ZeroOrMoreSegment {
287
+ bytes. push ( b'*' ) ;
268
288
}
269
- bytes. extend_from_slice ( s) ;
270
289
}
271
- Piece :: Parameter ( p , k ) => match p {
272
- Position :: Index ( _ , _ ) => {
273
- if * k == Kind :: OneOrMore {
274
- bytes. push ( b'+' ) ;
275
- } else if * k == Kind :: ZeroOrMore || * k == Kind :: ZeroOrMoreSegment {
276
- bytes. push ( b'* ' ) ;
290
+ Position :: Named ( n ) => match k {
291
+ Kind :: Normal | Kind :: Optional | Kind :: OptionalSegment => {
292
+ bytes . push ( b':' ) ;
293
+ bytes. extend_from_slice ( n ) ;
294
+ if * k == Kind :: Optional || * k == Kind :: OptionalSegment {
295
+ bytes. push ( b'? ' ) ;
277
296
}
278
297
}
279
- Position :: Named ( n) => match k {
280
- Kind :: Normal | Kind :: Optional | Kind :: OptionalSegment => {
281
- bytes. push ( b':' ) ;
282
- bytes. extend_from_slice ( n) ;
283
- if * k == Kind :: Optional || * k == Kind :: OptionalSegment {
284
- bytes. push ( b'?' ) ;
285
- }
286
- }
287
- Kind :: OneOrMore => {
288
- bytes. push ( b'+' ) ;
289
- bytes. extend_from_slice ( n) ;
290
- }
291
- Kind :: ZeroOrMore | Kind :: ZeroOrMoreSegment => {
292
- bytes. push ( b'*' ) ;
293
- bytes. extend_from_slice ( n) ;
294
- }
295
- } ,
298
+ Kind :: OneOrMore => {
299
+ bytes. push ( b'+' ) ;
300
+ bytes. extend_from_slice ( n) ;
301
+ }
302
+ Kind :: ZeroOrMore | Kind :: ZeroOrMoreSegment => {
303
+ bytes. push ( b'*' ) ;
304
+ bytes. extend_from_slice ( n) ;
305
+ }
296
306
} ,
297
- }
298
- }
307
+ } ,
308
+ } ) ;
299
309
300
310
String :: from_utf8_lossy ( & bytes) . to_string ( )
301
311
}
302
312
313
+ /// Returns the parameters of the current path.
303
314
pub fn params ( & self ) -> Vec < ( & ' a str , & ' b str ) > {
304
315
self . params_iter ( ) . collect ( )
305
316
}
306
317
318
+ /// Returns the parameters iterator of the current path.
307
319
pub fn params_iter < ' p > ( & ' p self ) -> ParamsIter < ' p , ' a , ' b , T > {
308
320
#[ inline]
309
321
fn piece_filter ( piece : & Piece ) -> Option < & ' _ str > {
@@ -328,11 +340,11 @@ impl<'a, 'b, T> Path<'a, 'b, T> {
328
340
}
329
341
}
330
342
331
- type FilterIter < ' a > =
332
- iter:: FilterMap < slice:: Iter < ' a , Piece > , fn ( piece : & ' a Piece ) -> Option < & ' a str > > ;
343
+ type FilterIter < ' a > = FilterMap < Iter < ' a , Piece > , fn ( piece : & ' a Piece ) -> Option < & ' a str > > ;
333
344
345
+ /// A Parameters Iterator.
334
346
pub struct ParamsIter < ' p , ' a , ' b , T > {
335
- iter : iter :: Zip < FilterIter < ' a > , std :: iter :: Copied < slice :: Iter < ' p , & ' b str > > > ,
347
+ iter : Zip < FilterIter < ' a > , Copied < Iter < ' p , & ' b str > > > ,
336
348
_t : PhantomData < T > ,
337
349
}
338
350
0 commit comments