viz-rs / path-tree

path-tree is a lightweight high performance HTTP request router for Rust
Apache License 2.0
119 stars 18 forks source link

chore: allow empty str #25

Closed fundon closed 1 year ago

fundon commented 1 year ago

19

@adriangb try this branch

adriangb commented 1 year ago

cc #26

fundon commented 1 year ago

@adriangb In this case:

Router(
  {
    "/users": Router(
      {
        "": users_endpoint,
        "/:name": user_endpoint,
       }
    )
)

Can the sub-router get the prefix path /users?

adriangb commented 1 year ago

Not easily. That same instance could be used somewhere else as well.

adriangb commented 1 year ago

I'm realizing I may have misunderstood. Just to clarify, I don't think the nested router should know/need to know that it is mounted under the prefix "/users".

With the example you gave I think the routing should be as follows:

/users -> Some(users_endpoint)
/users/ -> None
/users/abc123 -> Some(user_endpoint)
fundon commented 1 year ago

In writing, empty string can be used, but a full path can be assembled at the end.

Pseudo codes:

sub_router.path = ''
sub_router.routes = ['', '/:name']

parent_router.path = '/users'
parent_router.mount(sub_router)

fn mount(sub_router) {
   for route in sub_router.routes {
       let path = parent_router.path + route
       tree.insert(path)
   }
}
adriangb commented 1 year ago

Yeah that’s one way to do it. It’s nice to be able to support the version where the parent router doesn’t need to introspect into itself children