2 releases

new 0.1.1 Mar 31, 2025
0.1.0 Mar 17, 2025

#2409 in Procedural macros

Download history 97/week @ 2025-03-16 14/week @ 2025-03-23 104/week @ 2025-03-30

215 downloads per month

MIT license

130KB
2.5K SLoC

promkit-derive

Macro for promkit derives.

Convenient macros for form inputs

promkit-derive provides a Derive macro that simplifies interactive form input. By applying the #[derive(Promkit)] attribute to a struct, you can automatically generate interactive forms that populate your struct fields with user input.

Features

  • Automated Form Generation: The #[derive(Promkit)] macro automatically creates interactive form fields based on your struct definition
  • Type-Safe Conversion: Form inputs are automatically converted to the appropriate types for your struct fields
  • Customizable: Fine-tune the appearance and behavior of your forms with labels, styles, input modes, and more

Usage Example

use promkit::{crossterm::style::Color, style::StyleBuilder};
use promkit_derive::Promkit;

#[derive(Default, Debug, Promkit)]
struct Profile {
    #[form(
        label = "What is your name?",
        label_style = StyleBuilder::new().fgc(Color::DarkCyan).build(),
    )]
    name: String,

    #[form(default)]
    hobby: Option<String>,

    #[form(label = "How old are you?")]
    age: usize,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let mut profile = Profile::default();
    profile.build()?;  // This displays the interactive form and collects user input
    println!("{:?}", profile);  // The struct now contains the input values
    Ok(())
}

Attribute Options

The #[form(...)] attribute supports the following options:

  • label: Text label for the form field
  • label_style: Style settings for the label
  • active_char_style: Style for active characters
  • inactive_char_style: Style for inactive characters
  • mask: Masking character for password inputs
  • edit_mode: Edit mode settings
  • word_break_chars: Word break character settings
  • default: Use default settings (no options)

Supported Types

  • Basic types (String, usize, i32, etc.)
  • Option<T> types (becomes None if input is invalid)

How It Works

The #[derive(Promkit)] macro implements a build() method on your struct that:

  1. Generates text input fields for each field with a #[form] attribute
  2. Displays an interactive form to collect user input
  3. Converts the input values to the appropriate types and assigns them to your struct fields

This allows you to create interactive form inputs with minimal code, simply by defining your struct and its fields.

Dependencies

~5–14MB
~202K SLoC