Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ivowingelaar/uri_path_router
A proc-macro crate that generates flattened enums for routing
https://github.com/ivowingelaar/uri_path_router
Last synced: about 20 hours ago
JSON representation
A proc-macro crate that generates flattened enums for routing
- Host: GitHub
- URL: https://github.com/ivowingelaar/uri_path_router
- Owner: IvoWingelaar
- License: mit
- Created: 2022-06-02T09:56:52.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-06-02T10:47:46.000Z (over 2 years ago)
- Last Synced: 2024-12-09T05:41:54.532Z (26 days ago)
- Language: Rust
- Size: 20.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# uri_path_router
This library provides a procedural macro that uses a small domain specific
language to create a naive parser that can translate the path of a URI into a
variant of a flattened `enum` for routing purposes.The generated logic is basically a big nested `match` that greedily accepts an
input `&str`, splits it into segments separated by `/` characters, and outputs
a variant of the `enum`, optionally capturing segments of the path if they are
specified as variables.# Usage
You write this:
```rust
use uri_path_router::route;route! {
Router,
"foo" => VariantA,
"bar" => VariantBar {
"x" => BarX,
var => BarWithVariable(var)
},
"nested" / "syntax" / "demonstration" => Nested,
}
```The macro will produce a flattened `enum` like this:
```rust
pub enum Router<'a> {
VariantA,
VariantBar,
BarX,
BarWithVariable { var: &'a str },
Nested,
}
```Note that the `enum` only captures the variable when a variant specifies it,
and always does so as a borrow and as such without any allocations.
To convert a `&str` into a variant, use `TryFrom`:```rust
assert_eq!(Router::try_from("/foo"), Ok(Router::VariantA));
assert_eq!(Router::try_from("/bar"), Ok(Router::VariantBar));
assert_eq!(Router::try_from("/bar/x"), Ok(Router::BarX));
assert_eq!(
Router::try_from("/bar/not-x"),
Ok(Router::BarWithVariable { var: "not-x" })
);
assert_eq!(Router::try_from("/whatever"), Err(()));
assert_eq!(
Router::try_from("/nested/syntax/demonstration"),
Ok(Router::Nested)
);
```A nifty feature of this crate is that documentation is auto-generated for
every variant of the `enum` describing what pattern matches it.
You can check this out by hovering over the variant in your IDE (assuming you
have `rust-analyzer` or something similar configured to display tooltips), or
by running `cargo doc` on your crate and searching for the generated `enum`.# License
This library is provided under the MIT license. See [LICENSE](LICENSE).