https://github.com/swlkr/enum_router
Simple enum router for axum
https://github.com/swlkr/enum_router
Last synced: 9 months ago
JSON representation
Simple enum router for axum
- Host: GitHub
- URL: https://github.com/swlkr/enum_router
- Owner: swlkr
- Created: 2023-11-11T01:36:18.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2025-01-08T23:57:39.000Z (about 1 year ago)
- Last Synced: 2025-03-26T06:11:15.486Z (10 months ago)
- Language: Rust
- Homepage:
- Size: 43 KB
- Stars: 9
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# enum_router
enum_router is a rust enum router for axum.
```sh
cargo add --git https://github.com/swlkr/enum_router
```
# Declare your routes
```rust
use enum_router::router;
#[router]
pub enum Route {
#[get("/")]
Root,
#[get("/todos/{id}/edit")]
EditTodo(i32)
#[put("/todos/{id}")]
UpdateTodo(i32)
}
```
It will complain about missing functions which you still have to write:
```rust
async fn root() -> String {
Route::Root.to_string() // "/"
}
async fn edit_todo(Path(id): Path) -> String {
Route::EditTodo(id).to_string() // "/todos/{id}/edit"
}
async fn update_todo(Path(id): Path) -> String {
Route::UpdateTodo(id).to_string() // "/todos/{id}"
}
```
# Use it like this
```rust
#[tokio::main]
async fn main() {
let ip = "127.0.0.1:9001";
let listener = tokio::net::TcpListener::bind(ip).await.unwrap();
let router = Route::router();
axum::serve(listener, router).await.unwrap();
}
```
# Got state?
```rust
use std::sync::Arc;
use axum::extract::State;
struct AppState {
count: u64
}
#[router(Arc)]
enum Route {
#[get("/")]
Index
}
async fn index(State(_st): State>) -> String {
Route::Index.to_string()
}
#[tokio::main]
async fn main() {
let ip = "127.0.0.1:9001";
let listener = tokio::net::TcpListener::bind(ip).await.unwrap();
let router = Route::router().with_state(Arc::new(AppState { count: 0 }));
axum::serve(listener, router).await.unwrap();
}
```
# Resource routes
This crate does more than check your borrows, it now borrows a very productive feature from rails, resource routing!
```rust
#[router]
enum Route {
#[get("/")]
Index,
#[router]
Sessions(Sessions)
}
async fn index() -> String {
Route::Index.to_string()
}
#[resource]
pub enum Sessions {
Index, New, Create, Edit(i64), Update(i64)
}
impl Sessions {
async fn index() -> String {
Self::Index.to_string() // /sessions
}
async fn new() -> String {
Self::New.to_string() // /sessions/new
}
async fn create() -> String {
Self::Create.to_string() // /sessions
}
async fn edit(Path(id): Path) -> String {
Self::Edit(id).to_string() // /sessions/1/edit
}
async fn update(Path(id): Path) -> String {
Self::Update(id).to_string() // /sessions/1
}
}
```