| Crates.io | reinhardt-routers-macros |
| lib.rs | reinhardt-routers-macros |
| version | 0.1.0-alpha.1 |
| created_at | 2026-01-23 02:33:41.809411+00 |
| updated_at | 2026-01-23 02:33:41.809411+00 |
| description | Procedural macros for compile-time URL path validation in Reinhardt |
| homepage | |
| repository | https://github.com/kent8192/reinhardt-rs |
| max_upload_size | |
| id | 2063211 |
| size | 44,048 |
Procedural macros for URL routing in Reinhardt framework
reinhardt-routers-macros provides procedural macros to simplify URL routing definition in the Reinhardt framework, enabling compile-time route validation and type-safe URL pattern generation.
Add reinhardt to your Cargo.toml:
[dependencies]
reinhardt = { version = "0.1.0-alpha.1", features = ["urls-routers-macros"] }
# Or use a preset:
# reinhardt = { version = "0.1.0-alpha.1", features = ["standard"] } # Recommended
# reinhardt = { version = "0.1.0-alpha.1", features = ["full"] } # All features
Then import router macro features:
use reinhardt::urls::routers_macros::path;
Note: Router macro features are included in the standard and full feature presets.
The path! macro validates URL path syntax at compile time:
use reinhardt::urls::routers_macros::path;
// Simple static path
let users_path = path!("/users/");
assert_eq!(users_path, "/users/");
// Path with single parameter
let user_detail = path!("/users/{id}/");
assert_eq!(user_detail, "/users/{id}/");
// Path with multiple parameters
let user_post = path!("/users/{user_id}/posts/{post_id}/");
assert_eq!(user_post, "/users/{user_id}/posts/{post_id}/");
// Path with underscores in parameters
let profile_path = path!("/users/{user_id}/profile/");
assert_eq!(profile_path, "/users/{user_id}/profile/");
The path! macro accepts paths with:
Static segments:
path!("/api/v1/users/") // ✅ Simple static path
path!("/user-profiles/") // ✅ Hyphens allowed
path!("/files/document.pdf") // ✅ Dots allowed
path!("/api/v1.0/users/") // ✅ Version numbers
Dynamic parameters:
path!("/users/{id}/") // ✅ Single parameter
path!("/users/{user_id}/") // ✅ Snake_case parameter
path!("/users/{_id}/") // ✅ Leading underscore
path!("/users/{user_id_123}/") // ✅ Numbers in parameter
path!("/users/{user_id}/posts/{post_id}/") // ✅ Multiple parameters
path!("/items/{item_id}/details/") // ✅ Parameter in middle
The following patterns will produce compile-time errors:
Missing leading slash:
path!("users/") // ❌ Error: URL path must start with '/'
Invalid parameter names:
path!("/users/{userId}/") // ❌ Error: Parameter names must be snake_case
path!("/users/{user-id}/") // ❌ Error: Hyphens not allowed in parameters
path!("/users/{UserId}/") // ❌ Error: Uppercase not allowed
Empty parameters:
path!("/users/{}/") // ❌ Error: Empty parameter name
Unmatched braces:
path!("/users/{id/") // ❌ Error: Unmatched '{'
path!("/users/id}/") // ❌ Error: Unmatched '}'
Nested parameters:
path!("/users/{{id}}/") // ❌ Error: Parameters cannot be nested
Double slashes:
path!("/users//posts/") // ❌ Error: Paths should not contain consecutive slashes
Invalid characters:
path!("/users/{id}?query=1") // ❌ Error: Invalid character '?' (query strings not in path)
path!("/users/{id}#section") // ❌ Error: Invalid character '#' (fragments not in path)
path! Macromacro_rules! path {
($path:literal) => { ... };
}
Parameters:
$path: literal - String literal containing the URL path patternReturns:
&'static str - Validated path string literal (same as input if valid)Validation Rules:
Path Structure:
/// (except after protocol, which is not applicable here)Parameter Format:
{}a-z or _)a-z, 0-9, _){{inner}} is invalid)Path Characters (outside parameters):
a-z, A-Z, 0-9-, _, /, ., *Compile-Time Error Messages:
The macro provides detailed error messages with examples:
MustStartWithSlash - Path doesn't start with /UnmatchedOpenBrace - Missing closing }UnmatchedCloseBrace - Missing opening {EmptyParameterName - Empty {} foundInvalidParameterName - Parameter not in snake_case formatDoubleSlash - Consecutive slashes foundInvalidCharacter - Character not allowed in pathNestedParameters - Nested braces foundThe path! macro is typically used when defining routes:
use reinhardt::urls::routers_macros::path;
// Define route patterns
let list_path = path!("/users/");
let detail_path = path!("/users/{id}/");
let nested_path = path!("/users/{user_id}/posts/{post_id}/");
// Use with router (example)
// router.add(list_path, handler);
// router.add(detail_path, detail_handler);
All validation errors are caught at compile time, not runtime. This ensures:
cargo buildExample Error Output:
error: URL path must start with '/'
Example: path!("/users/") instead of path!("users/")
--> src/main.rs:5:18
|
5 | let path = path!("users/");
| ^^^^^^^^^
Current Implementation:
path! macro is implementedLicensed under either of Apache License, Version 2.0 or MIT license at your option.