https://github.com/bschwind/app-route
Treat application routes (URL path + query string) as strongly-typed Rust structs
https://github.com/bschwind/app-route
Last synced: 8 months ago
JSON representation
Treat application routes (URL path + query string) as strongly-typed Rust structs
- Host: GitHub
- URL: https://github.com/bschwind/app-route
- Owner: bschwind
- License: mit
- Created: 2019-04-26T09:30:53.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-16T03:38:45.000Z (almost 7 years ago)
- Last Synced: 2024-10-31T13:50:49.543Z (over 1 year ago)
- Language: Rust
- Homepage:
- Size: 20.5 KB
- Stars: 13
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
AppRoute
=======
Treat application routes (URL path + query string) as strongly-typed Rust structs
Dependencies
------------
- cargo
- rustc
Build
-----
$ cargo build
Test
-----------
$ cargo test
Benchmark
-----------
$ cargo bench
Example Code
------------
`src/Cargo.toml`
```toml
[dependencies]
app_route = "0.1"
serde = { version = "1.0", features = ["derive"] }
```
`main.rs`
```rust
use app_route::AppRoute;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq)]
struct UserListQuery {
limit: Option,
offset: Option,
keyword: Option,
#[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
}
```
TODO
----
- [ ] URL Hash Fragments
- [x] Support trailing wildcard as a path param
- [ ] Make the AppRoute trait object-safe if possible
- [ ] Use spans properly in the procedural macro so errors actually make sense