/informal

Simple Rust crate for parsing user input

Primary LanguageRustApache License 2.0Apache-2.0

Informal

Simple crate for parsing user input.

Getting started

Add the following dependency to your Cargo.toml.

[dependencies]
informal = "0.3"

Or run the command.

cargo add informal

Usage

Rust type inference is used to know what to return.

let username: String = informal::prompt("Please enter your name: ").get();

FromStr is used to parse the input, so you can read any type that implements FromStr.

let age: u32 = informal::prompt("Please enter your age: ").get();

.matches() can be used to validate the input data.

let age: u32 = informal::prompt("Please enter your age again: ")
    .matches(|x| *x < 120)
    .get();

.type_error_message() can be used to specify an error message when the string fails to be converted into the wanted type.

let age: u32 = informal::prompt("Please enter your age: ")
    .type_error_message("Error: What kind of age is that?!")
    .get();

.validator_error_message() can be used to specify an error message when your matches condition does not hold.

let age: u32 = informal::prompt("Please enter your age: ")
    .matches(|x| *x < 120)
    .validator_error_message("Error: You can't be that old.... can you?")
    .get();

A convenience function confirm is provided for getting a yes or no answer.

if informal::confirm("Are you sure you want to continue?") {
    // continue
} else {
    panic!("Aborted!");
}

A convenience function confirm_with_message is provided for getting a yes or no answer with an error message.

if informal::confirm_with_message("Are you sure you want to continue?", "Please answer with 'yes' or 'no'") {
    // continue
} else {
    panic!("Aborted!");
}

License

Licensed under either of

at your option.