ibuilder
Interactive builders for structs.
This crate provides a way to construct structs interactively, starting from an "empty" state and filling the values of the fields of the struct prompting the user with multiple choices and text inputs. After each choice the internal state of the builder changes.
The builder provides the user with interactive menu-like interfaces, keeping the UI abstract and rust type-safeness.
The API of this crate is very simple, you start with a struct derived from IBuilder
and call
the auto-generated function builder()
from the Buildable
trait. This will construct a new
custom-built Builder
to use for the communication. The Builder
provides two main functions:
get_options()
for getting the current state of the builder and the list of possible options
the user can choose, and choose(input)
that validates and inserts the choice of the user.
Rationale
When building an interactive application (e.g. a Telegram Bot or a console application) which needs loads of configurations it can be pretty hard to come out with a decent interface without writing loads of code for handling all the corner cases.
This crates provides a useful abstraction that takes care of the management of the abstract interface while keeping the API clean. The struct where you needs the data is the actual output of this crate, keeping all the type-safeness.
The derive API is inspired by the great structopt
crate.
Supported features
- Deriving any struct with named fields (or with one unnamed field like
struct Foo(i64)
) - Default values for the fields
- Custom message prompt for fields, structs, enums and variants
- Renaming fields, structs and variants for better looking options
- Hidden fields (that takes the value only from the default)
- Nested structures (i.e. custom types)
- Enums (also with variants with field, but only one if unnamed)
- Supported field types: all numeric types from rust,
bool
,String
,char
,Box<T>
,Vec<T>
andOption<T>
- Any field type that implementes the
NewBuildableValue
trait
Example of usage
use *;
let mut builder = builder;
let options = builder.get_options; // main menu: select the field to edit
builder.choose.unwrap; // select the field
let options = builder.get_options; // int_field menu
assert!; // for inserting the integer value
builder.choose.unwrap; // insert the value
let options = builder.get_options; // back to the main menu
builder.choose.unwrap; // select the second field
let options = builder.get_options; // string_field menu
assert!; // for inserting the string value
builder.choose.unwrap; // insert the value
assert!;
let options = builder.get_options; // main menu again, but the "Done" option is available
// chose the "Done" option, the return value is Ok(Some(Example))
let value = builder.choose.unwrap.unwrap;
assert_eq!;
assert_eq!;
assert_eq!;
License: MIT