Causes requests which start with `path` to be processed by `sub_router`. `sub_router` will be used by stripping the leading `path` from the path of the request. Panics if `self` already has any handlers registered on paths which start with `path`. Panics if the `path` is [invalid](Self#paths).
(self, path: impl Into<String>, sub_router: Self)
| 339 | /// |
| 340 | /// Panics if the `path` is [invalid](Self#paths). |
| 341 | pub fn nest(self, path: impl Into<String>, sub_router: Self) -> Self { |
| 342 | let path = path.into(); |
| 343 | assert_valid_path(&path); |
| 344 | |
| 345 | // FIXME: either this check is too restrictive, or the checks in the other methods are too lenient. |
| 346 | // Do we want it to be the case that the `sub_router` effectively takes ownership of the whole route below `path`, |
| 347 | // or just the routes it actually contains? |
| 348 | if self.routes.iter().any(|route| route.path.starts_with(&path)) { |
| 349 | panic!("Cannot nest router at `{path}`; existing routes overlap with nested path"); |
| 350 | } |
| 351 | |
| 352 | let mut merged = self; |
| 353 | for route in sub_router.routes { |
| 354 | let nested_path = join_paths(&path, &route.path); |
| 355 | merged = merged.add_route(route.method, nested_path, route.handler); |
| 356 | } |
| 357 | merged |
| 358 | } |
| 359 | |
| 360 | /// Combines all of the routes in `self` and `other_router` into a single [`Router`]. |
| 361 | /// |
no test coverage detected