kstool-helper-generator 0.7.1

A macro help user create mpsc communications and other
Documentation

helper-generator

A Rust procedural macro library that automates the creation of MPSC (Multi-Producer, Single-Consumer) message channel helpers and related utilities. It significantly reduces boilerplate code when building concurrent, message-passing systems using Tokio's async runtime.

Installation

Add to your Cargo.toml:

[dependencies]
kstool-helper-generator = "0.7.0"

Features

This library provides three procedural macros:

#[derive(Helper)] - MPSC Communication Helper

Automatically generates a helper struct with methods to send enum variants through an MPSC channel.

use kstool_helper_generator::Helper;

#[derive(Helper)]
enum MyEvent {
    UserLogin { username: String },
    UserLogout,
    DataUpdate(Vec<u8>),
}

// Generated:
// - MyHelper struct with async send methods
// - MyEventReceiver type alias
// - MyHelper::new(channel_size) constructor

Generated methods:

impl MyHelper {
    pub fn new(buffer: usize) -> (Self, MyEventReceiver) { /* ... */ }
    pub async fn user_login(&self, username: String) -> Option<()> { /* ... */ }
    pub async fn user_logout(&self) -> Option<()> { /* ... */ }
    pub async fn data_update(&self, field_0: Vec<u8>) -> Option<()> { /* ... */ }
}

Attributes:

  • #[helper(block)] - Generate blocking send methods in addition to async
  • #[helper(no_async)] - Generate only blocking methods (no async)
  • Per-variant attributes to override behavior

#[oneshot_helper] - Request-Response Communication

Extends Helper to support request-response patterns using oneshot channels.

use kstool_helper_generator::oneshot_helper;

#[oneshot_helper]
enum QueryEvent {
    #[ret(String)]
    GetConfig { key: String },
    #[ret(bool)]
    IsEnabled { feature: &'static str },
}

Methods return the response type wrapped in Option:

impl QueryHelper {
    pub async fn get_config(&self, key: String) -> Option<String> { /* ... */ }
    pub async fn is_enabled(&self, feature: &'static str) -> Option<bool> { /* ... */ }
}

#[bit_helper] - Bit Flag Utilities

Generates bit flag enums with checker methods.

use kstool_helper_generator::bit_helper;

#[bit_helper]
enum Features {
    Analytics,      // = 1
    Notifications,  // = 2
    DarkMode,       // = 4
}

Generated methods:

impl Features {
    pub fn is_analytics(&self) -> bool { /* ... */ }
    pub fn is_notifications(&self) -> bool { /* ... */ }
    pub fn is_dark_mode(&self) -> bool { /* ... */ }
}

Naming Convention

Enum variant names are automatically converted to snake_case for method names:

Variant Name Method Name
UserLogin user_login()
GetHTTPResponse get_http_response()
XMLParser xml_parser()

Requirements

  • The enum name must contain "Event" for Helper and oneshot_helper macros (e.g., MyEvent, AppEvent)
  • Tokio runtime for async methods

License

AGPL-3.0-or-later

Author

KunoiSayami