goodrouter 1.0.11

a bi directional trie router for all your routing needs
Documentation

Goodrouter, the rust edition

A good router should:

  • work in a server or a client (or any other) environment
  • be able to construct routes based on their name
  • should have a simple API!
  • not do the actual navigation!
  • be framework agnostic
  • be very minimal and simple!

Check out our (website)[https://www.goodrouter.org], join our (Discord server)[https://discord.gg/BJ8v7xTq8d]!

Example

let mut router = Router::new();

router
    .insert_route("all-products", "/product/all")
    .insert_route("product-detail", "/product/{id}");

// And now we can parse routes!

{
    let (route_key, route_parameters) = router.parse_route("/not-found");
    assert_eq!(route_key, None);
    assert_eq!(route_parameters, vec![].into_iter().collect());
}

{
    let (route_key, route_parameters) = router.parse_route("/product/all");
    assert_eq!(route_key, Some("all-products"));
    assert_eq!(route_parameters, vec![].into_iter().collect());
}

{
    let (route_key, route_parameters) = router.parse_route("/product/1");
    assert_eq!(route_key, Some("product-detail"));
    assert_eq!(
        route_parameters,
        vec![("id", "1")]
            .into_iter()
            .map(|(k, v)| (k, Cow::Borrowed(v)))
            .collect()
    );
}

// And we can stringify routes

{
    let route_parameters: HashMap<_, _> = vec![].into_iter().collect();
    let path = router.stringify_route("all-products", &route_parameters);
    assert_eq!(path.unwrap().into_owned(), "/product/all".to_owned());
}

{
    let route_parameters: HashMap<_, _> = vec![("id", "2")].into_iter().collect();
    let path = router.stringify_route("product-detail", &route_parameters);
    assert_eq!(path.unwrap().into_owned(), "/product/2".to_owned());
}