Crate rusttoolkit

Source
Expand description

§rusttoolkit - Universal Rust Meta-Programming Toolkit

Provides the for_each! macro for eliminating repetitive code patterns through powerful iteration with built-in case transformation support.

§Features

  • Multiple item types: Identifiers, strings, numbers, and arrays
  • Array indexing: Access elements with %{param[0]}, %{param[1]}, etc.
  • Case transformations: Built-in support for snake_case, camelCase, kebab-case, PascalCase, UPPER_CASE, and more
  • Clean syntax: Uses %{param} and %{param:transform} to avoid conflicts with Rust syntax
  • Template flexibility: Multiple parameter references and transforms in same template
  • Production ready: Comprehensive test coverage and optimized implementation

§Quick Start

use rusttoolkit::for_each;

// Single items - generates error(), warn(), info() functions
for_each!([error, warn, info], |level| {
    pub fn %{level}(msg: &str) {
        println!("[{}] {}", stringify!(%{level}).to_uppercase(), msg);
    }
});

// Array items - generates status_GET(), status_POST() functions  
for_each!([["GET", 200], ["POST", 201]], |req| {
    pub fn status_%{req[0]}() -> u16 {
        %{req[1]}
    }
});

// Case transformations - generates get_user_data_log!(), create_post_log!() macros
for_each!([getUserData, createPost], |method| {
    macro_rules! %{method:snake}_log {
        ($msg:expr) => {
            format!("[{}] {}", "%{method:upper}", $msg)
        };
    }
});

§Template Syntax - %{} Notation

The %{} notation is our special template syntax that gets replaced during macro expansion:

§Basic Usage

  • %{param} - Replace with single item value
  • %{param[0]} - Replace with first array element
  • %{param[1]} - Replace with second array element
  • Multiple references supported in same template

§Case Transformations

  • %{param:snake} - Convert to snake_case: getUserData → get_user_data
  • %{param:camel} - Convert to camelCase: getUserData → getUserData
  • %{param:kebab} - Convert to kebab-case: getUserData → get-user-data
  • %{param:pascal} - Convert to PascalCase: getUserData → GetUserData
  • %{param:title} - Convert to Title Case: getUserData → Get User Data
  • %{param:upper} - Convert to UPPERCASE: getUserData → GETUSERDATA
  • %{param:lower} - Convert to lowercase: getUserData → getuserdata
  • %{param:reverse} - Reverse string: getUserData → ataDresUteg
  • %{param:len} - Get string length: getUserData → 11

Why %{}? We chose this syntax to avoid conflicts with Rust’s native syntax:

  • Doesn’t conflict with Rust 2024’s prefix identifier parsing (#identifier)
  • Visually distinct from standard Rust syntax
  • Allows natural-looking templates that remain readable
  • Supports complex expressions like %{param[0]:snake} for array indexing with transforms

§Supported Item Types

  • Identifiers: error, warn, info
  • Strings: "GET", "POST", "PUT"
  • Numbers: 200, 404, 500
  • Arrays: ["GET", 200], ["POST", 201]
  • Mixed: [error, "GET", 200]

Macros§

for_each
Universal iteration macro supporting single items and arrays.