@@ -173,11 +173,12 @@ impl Router {
173
173
. ok_or_else ( || anyhow ! ( "Cannot match route for path {path}" ) ) ?;
174
174
175
175
let route_handler = best_match. handler ( ) ;
176
+ let captures = best_match. captures ( ) ;
176
177
177
178
Ok ( RouteMatch {
178
179
inner : RouteMatchKind :: Real {
179
180
route_handler,
180
- best_match ,
181
+ captures ,
181
182
path,
182
183
} ,
183
184
} )
@@ -253,13 +254,12 @@ impl<'router, 'path> RouteMatch<'router, 'path> {
253
254
}
254
255
255
256
/// The matched route, excluding any trailing wildcard, combined with the base.
256
- pub fn based_route_or_prefix ( & self ) -> String {
257
+ pub fn based_route_or_prefix ( & self ) -> & str {
257
258
self . inner
258
259
. route_handler ( )
259
260
. based_route
260
261
. strip_suffix ( "/..." )
261
262
. unwrap_or ( & self . inner . route_handler ( ) . based_route )
262
- . to_string ( )
263
263
}
264
264
265
265
/// The matched route, as originally written in the manifest.
@@ -268,22 +268,21 @@ impl<'router, 'path> RouteMatch<'router, 'path> {
268
268
}
269
269
270
270
/// The matched route, excluding any trailing wildcard.
271
- pub fn raw_route_or_prefix ( & self ) -> String {
271
+ pub fn raw_route_or_prefix ( & self ) -> & str {
272
272
self . inner
273
273
. route_handler ( )
274
274
. raw_route
275
275
. strip_suffix ( "/..." )
276
276
. unwrap_or ( & self . inner . route_handler ( ) . raw_route )
277
- . to_string ( )
278
277
}
279
278
280
279
/// The named wildcards captured from the path, if any
281
- pub fn named_wildcards ( & self ) -> HashMap < String , String > {
280
+ pub fn named_wildcards ( & self ) -> HashMap < & str , & str > {
282
281
self . inner . named_wildcards ( )
283
282
}
284
283
285
284
/// The trailing wildcard part of the path, if any
286
- pub fn trailing_wildcard ( & self ) -> String {
285
+ pub fn trailing_wildcard ( & self ) -> Cow < ' _ , str > {
287
286
self . inner . trailing_wildcard ( )
288
287
}
289
288
}
@@ -304,7 +303,7 @@ enum RouteMatchKind<'router, 'path> {
304
303
/// The route handler that matched the path.
305
304
route_handler : & ' router RouteHandler ,
306
305
/// The best match for the path.
307
- best_match : routefinder:: Match < ' router , ' path , RouteHandler > ,
306
+ captures : routefinder:: Captures < ' router , ' path > ,
308
307
/// The path that was matched.
309
308
path : & ' path str ,
310
309
} ,
@@ -320,44 +319,37 @@ impl<'router, 'path> RouteMatchKind<'router, 'path> {
320
319
}
321
320
322
321
/// The named wildcards captured from the path, if any
323
- pub fn named_wildcards ( & self ) -> HashMap < String , String > {
324
- let Self :: Real { best_match , .. } = & self else {
322
+ pub fn named_wildcards ( & self ) -> HashMap < & str , & str > {
323
+ let Self :: Real { captures , .. } = & self else {
325
324
return HashMap :: new ( ) ;
326
325
} ;
327
- best_match
328
- . captures ( )
329
- . iter ( )
330
- . map ( |( k, v) | ( k. to_owned ( ) , v. to_owned ( ) ) )
331
- . collect ( )
326
+ captures. iter ( ) . collect ( )
332
327
}
333
328
334
329
/// The trailing wildcard part of the path, if any
335
- pub fn trailing_wildcard ( & self ) -> String {
336
- let ( best_match , path) = match self {
330
+ pub fn trailing_wildcard ( & self ) -> Cow < ' _ , str > {
331
+ let ( captures , path) = match self {
337
332
// If we have a synthetic match, we already have the trailing wildcard.
338
333
Self :: Synthetic {
339
334
trailing_wildcard, ..
340
- } => return trailing_wildcard. clone ( ) ,
341
- Self :: Real {
342
- best_match, path, ..
343
- } => ( best_match, path) ,
335
+ } => return trailing_wildcard. into ( ) ,
336
+ Self :: Real { captures, path, .. } => ( captures, path) ,
344
337
} ;
345
338
346
- best_match
347
- . captures ( )
339
+ captures
348
340
. wildcard ( )
349
341
. map ( |s|
350
342
// Backward compatibility considerations - Spin has traditionally
351
343
// captured trailing slashes, but routefinder does not.
352
344
match ( s. is_empty ( ) , path. ends_with ( '/' ) ) {
353
345
// route: /foo/..., path: /foo
354
- ( true , false ) => s. to_owned ( ) ,
346
+ ( true , false ) => s. into ( ) ,
355
347
// route: /foo/..., path: /foo/
356
- ( true , true ) => "/" . to_owned ( ) ,
348
+ ( true , true ) => "/" . into ( ) ,
357
349
// route: /foo/..., path: /foo/bar
358
- ( false , false ) => format ! ( "/{s}" ) ,
350
+ ( false , false ) => format ! ( "/{s}" ) . into ( ) ,
359
351
// route: /foo/..., path: /foo/bar/
360
- ( false , true ) => format ! ( "/{s}/" ) ,
352
+ ( false , true ) => format ! ( "/{s}/" ) . into ( ) ,
361
353
} )
362
354
. unwrap_or_default ( )
363
355
}
0 commit comments