Crates.io | app_route_derive |
lib.rs | app_route_derive |
version | 0.3.0 |
source | src |
created_at | 2019-04-26 09:41:40.711422 |
updated_at | 2019-06-16 03:23:28.815621 |
description | A procedural macro to allow automatic deserialization of URL path + query parameters into Rust structs |
homepage | |
repository | https://github.com/bschwind/app-route |
max_upload_size | |
id | 130287 |
size | 14,565 |
Treat application routes (URL path + query string) as strongly-typed Rust structs
$ cargo build
$ cargo test
$ cargo bench
src/Cargo.toml
[dependencies]
app_route = "0.1"
serde = { version = "1.0", features = ["derive"] }
main.rs
use app_route::AppRoute;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct UserListQuery {
limit: Option<u64>,
offset: Option<u64>,
keyword: Option<String>,
#[serde(default)]
friends_only: bool,
}
#[derive(AppRoute, Debug, PartialEq)]
#[route("/groups/:group_id/users")]
struct UsersListRoute {
group_id: u64,
#[query]
query: UserListQuery,
}
fn main() {
let path: UsersListRoute =
"/groups/4313145/users?offset=10&limit=20&friends_only=false&keyword=some_keyword"
.parse()
.unwrap();
assert_eq!(
path,
UsersListRoute {
group_id: 4313145,
query: {
UserListQuery {
limit: Some(20),
offset: Some(10),
keyword: Some("some_keyword".to_string()),
friends_only: false,
}
}
}
);
println!("Path: {}", path);
// Output:
// Path: /groups/4313145/users?limit=20&offset=10&keyword=some_keyword&friends_only=false
}