2 releases
new 0.1.1 | Mar 31, 2025 |
---|---|
0.1.0 | Mar 17, 2025 |
#2409 in Procedural macros
215 downloads per month
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 fieldlabel_style
: Style settings for the labelactive_char_style
: Style for active charactersinactive_char_style
: Style for inactive charactersmask
: Masking character for password inputsedit_mode
: Edit mode settingsword_break_chars
: Word break character settingsdefault
: Use default settings (no options)
Supported Types
- Basic types (
String
,usize
,i32
, etc.) Option<T>
types (becomesNone
if input is invalid)
How It Works
The #[derive(Promkit)]
macro implements a build()
method on your struct that:
- Generates text input fields for each field with a
#[form]
attribute - Displays an interactive form to collect user input
- 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