2 个版本

0.2.1 2022 年 10 月 19 日
0.2.0 2022 年 10 月 19 日

#41 in #segment

MIT 许可证

11KB
248 行代码(不包括注释)

嵌套路由器

嵌套路由器的路径段匹配。

如何使用

  1. 定义你的路由

    fn route_list_1() -> (RouteList, RouteList) {
        let sub = RouteList {
            routes: vec![Route {
                path: ":id".to_string(),
                has_sub_routes: false,
            }],
        };
        let root = RouteList {
            routes: vec![
                Route {
                    path: ":id".to_string(),
                    has_sub_routes: true,
                },
                Route {
                    path: "about".to_string(),
                    has_sub_routes: false,
                },
            ],
        };
        (root, sub)
    }
    
  2. 使用两个路由器

    let (root, sub) = route_list_1();
    
    let absolute_path = "/123/456";
    let relative_path = &absolute_path[1..];
    
    let RouteOutput {
        sub_path,
        route,
        params,
    } = root.route(relative_path).unwrap();
    assert_eq!(route.path, ":id");
    assert_eq!(params.get("id"), Some(&"123".to_string()));
    
    // Your business logic here for the first route
    
    let RouteOutput {
        sub_path,
        route,
        params,
    } = sub.route(&sub_path.unwrap()).unwrap();
    assert_eq!(route.path, ":id");
    assert_eq!(params.get("id"), Some(&"456".to_string()));
    assert_eq!(sub_path, None);
    
    // Your business logic here for the second route
    

限制

  • Route::path 应该不捕获名为 "_sub_path" 的通配符 (crate::SUB_PATH_WILDCARD_NAME)。

依赖关系

~39KB