struct-input 0.1.2

A Rust crate for mapping interactive CLI user input directly to structs with declarative validation and async support.
Documentation

struct-input

A Rust crate for easy CLI input mapping to structs.

struct-input provides a StructInput trait that can be derived for any struct. This trait allows you to easily read CLI input and map it to the fields of your struct.

Features

  • Easy to use derive macro for the StructInput trait.
  • Supports reading strings, integers, and optional strings.
  • Customizable input prompts.

Usage

Add struct-input and struct-input-derive to your Cargo.toml:

[dependencies]
struct-input = "0.1.2"

Then, derive the StructInput trait for your struct:

use struct_input::StructInput;

#[derive(StructInput)]
struct MyStruct {
    #[struct_input(prompt = "Enter your name: ")]
    name: String,
    #[struct_input(prompt = "Enter your age: ")]
    age: i32,
    #[struct_input(prompt = "Enter your favorite color (optional): ")]
    favorite_color: Option<String>,
}

fn main() {
    let my_struct = MyStruct::from_cli();
    println!("Hello, {}! You are {} years old.", my_struct.name, my_struct.age);
    if let Some(color) = my_struct.favorite_color {
        println!("Your favorite color is {}.", color);
    }
}

License

This project is licensed under the MIT License - see the LICENSE file for details.