Crates.io | route |
lib.rs | route |
version | 0.2.0 |
source | src |
created_at | 2016-10-29 04:43:56.522849 |
updated_at | 2016-10-30 02:39:48.188543 |
description | Route URL paths with safe parameter extraction |
homepage | |
repository | https://github.com/uniphil/route-rs |
max_upload_size | |
id | 7031 |
size | 8,745 |
This crate is my attempt at a safe helper for mapping URL routes to handlers for rust web applications.
There are several routing libraries already available, but all of the ones I have been able to find share a common problem: path patterns are defined as strings, and URL parameters get parsed out at runtime and stashed in some kind of Map
that handlers have to .get()
and .unwrap()
to access. I want to extract parameters without unwrapping, and I want rust's type system to ensure that I'm not making mistakes!
in Cargo.toml:
[dependencies]
route = "0.2.0"
route
just exports a the macro route!
, so you need to #[macro use]
it:
#[macro use]
extern crate route;
Suppose you have some HTTP request/response server setup like
// imaginary request/response structs provided by the framework:
struct Request<'a> {
path: &'a str,
}
type Response = String;
// application handlers that we need to route:
// Note that some handlers take extra parameters that we hope to fill from the path!
fn home(req: &Request) -> Response {
"home".to_string()
}
fn blog_post(req: &Request, id: u32) -> Response {
format!("blog: {}", id)
}
fn account(req: &Request, subpath: &str) -> Response {
format!("account -- subpath: {}", subpath)
}
Then you could set up a routing handler like:
fn handle_route(req: &Request) -> Response {
route!(req.path, {
(/) => home(req);
(/"blog"/[id: u32]) => blog_post(req, id);
(/"me"[/rest..]) => account(req, rest);
});
Response::from("not found")
}
And you're set!
assert_eq!(&handle_route(&Request { path: "/" }), "home");
assert_eq!(&handle_route(&Request { path: "/blog/42" }), "blog: 42");
assert_eq!(&handle_route(&Request { path: "/me/a/b/c" }), "account -- subpath: /a/b/c");
assert_eq!(&handle_route(&Request { path: "/foo" }), "not found");