| Crates.io | rusttoolkit |
| lib.rs | rusttoolkit |
| version | 0.1.0 |
| created_at | 2025-08-07 14:51:47.026418+00 |
| updated_at | 2025-08-07 14:51:47.026418+00 |
| description | Rust toolkit with cool useful procedural macros |
| homepage | https://github.com/deepbrain/rusttoolkit |
| repository | https://github.com/deepbrain/rusttoolkit |
| max_upload_size | |
| id | 1785407 |
| size | 843,262 |
Rust meta-programming toolkit with advanced code generation macros.
for_each! macro: Eliminate repetitive code patterns with powerful iteration%{param[0]}, %{param[1]}, etc.%{param} to avoid conflicts with Rust syntaxAdd to your Cargo.toml:
[dependencies]
rusttoolkit = "0.1.0"
use rusttoolkit::for_each;
for_each!([error, warn, info], |level| {
pub fn %{level}(msg: &str) {
println!("[{}] {}", stringify!(%{level}).to_uppercase(), msg);
}
});
// Generates:
// pub fn error(msg: &str) { ... }
// pub fn warn(msg: &str) { ... }
// pub fn info(msg: &str) { ... }
// Usage:
error("Something went wrong"); // Prints: [ERROR] Something went wrong
warn("This is a warning"); // Prints: [WARN] This is a warning
info("Just informing you"); // Prints: [INFO] Just informing you
for_each!(["GET", "POST", "PUT"], |method| {
pub fn handle_%{method}() -> &'static str {
"%{method}"
}
});
// Generates:
// pub fn handle_GET() -> &'static str { "GET" }
// pub fn handle_POST() -> &'static str { "POST" }
// pub fn handle_PUT() -> &'static str { "PUT" }
// Usage:
handle_GET() // Returns: "GET"
handle_POST() // Returns: "POST"
handle_PUT() // Returns: "PUT"
for_each!([200, 404, 500], |code| {
pub fn status_%{code}() -> u16 {
%{code}
}
});
// Generates:
// pub fn status_200() -> u16 { 200 }
// pub fn status_404() -> u16 { 404 }
// pub fn status_500() -> u16 { 500 }
// Usage:
status_200() // Returns: 200
status_404() // Returns: 404
status_500() // Returns: 500
for_each!([["GET", 200], ["POST", 201]], |req| {
pub fn status_%{req[0]}() -> u16 {
%{req[1]}
}
});
// Generates:
// pub fn status_GET() -> u16 { 200 }
// pub fn status_POST() -> u16 { 201 }
// Usage:
status_GET() // Returns: 200
status_POST() // Returns: 201
for_each!([error, "GET", 200], |item| {
pub fn mixed_%{item}() -> &'static str {
stringify!(%{item})
}
});
// Generates:
// pub fn mixed_error() -> &'static str { "error" }
// pub fn mixed_GET() -> &'static str { "GET" }
// pub fn mixed_200() -> &'static str { "200" }
// Usage:
mixed_error() // Returns: "error"
mixed_GET() // Returns: "GET"
mixed_200() // Returns: "200"
for_each!([debug, info], |level| {
pub fn %{level}_log_%{level}() -> &'static str {
concat!(stringify!(%{level}), "_", stringify!(%{level}))
}
});
// Generates:
// pub fn debug_log_debug() -> &'static str { "debug_debug" }
// pub fn info_log_info() -> &'static str { "info_info" }
// Usage:
debug_log_debug() // Returns: "debug_debug"
info_log_info() // Returns: "info_info"
for_each!([["users", "GET", "/api/users"], ["posts", "POST", "/api/posts"]], |route| {
pub fn %{route[0]}_%{route[1]}() -> &'static str {
"%{route[2]}"
}
});
// Generates:
// pub fn users_GET() -> &'static str { "/api/users" }
// pub fn posts_POST() -> &'static str { "/api/posts" }
// Usage:
users_GET() // Returns: "/api/users"
posts_POST() // Returns: "/api/posts"
for_each!([error, warn, info], |level| {
macro_rules! %{level}_log {
($msg:expr) => {
format!("[{}] {}", stringify!(%{level}).to_uppercase(), $msg)
};
}
});
// Generates:
// macro_rules! error_log { ... }
// macro_rules! warn_log { ... }
// macro_rules! info_log { ... }
// Usage:
error_log!("failed") // Returns: "[ERROR] failed"
warn_log!("warning") // Returns: "[WARN] warning"
info_log!("started") // Returns: "[INFO] started"
for_each!([["create", "user"], ["delete", "post"]], |action| {
macro_rules! %{action[0]}_%{action[1]}_macro {
($id:expr) => {
format!("{}_{}_action: {}",
stringify!(%{action[0]}),
stringify!(%{action[1]}),
$id)
};
}
});
// Generates:
// macro_rules! create_user_macro { ... }
// macro_rules! delete_post_macro { ... }
// Usage:
create_user_macro!(123) // Returns: "create_user_action: 123"
delete_post_macro!(456) // Returns: "delete_post_action: 456"
error, warn, info"GET", "POST", "PUT"200, 404, 500["GET", 200], ["POST", 201][error, "GET", 200]%{} NotationThe %{} notation is our special template syntax that gets replaced during macro expansion:
%{param} - Replace with single item value%{param[0]} - Replace with first array element%{param[1]} - Replace with second array elementWhy %{}? We chose this syntax to avoid conflicts with Rust's native syntax:
#identifier)%{param[0]} for array indexingLicensed under either of
at your option.
Contributions are welcome! Please feel free to submit a Pull Request.