Trait read_input::InputBuild[][src]

pub trait InputBuild<T: FromStr> {
    fn msg(self, msg: impl ToString) -> Self;
fn repeat_msg(self, msg: impl ToString) -> Self;
fn err(self, err: impl ToString) -> Self;
fn add_test<F: Fn(&T) -> bool + 'static>(self, test: F) -> Self;
fn add_err_test<F>(self, test: F, err: impl ToString) -> Self
    where
        F: Fn(&T) -> bool + 'static
;
fn clear_tests(self) -> Self;
fn err_match<F>(self, err_match: F) -> Self
    where
        F: Fn(&T::Err) -> Option<String> + 'static
;
fn inside<U: InsideFunc<T>>(self, constraint: U) -> Self;
fn inside_err<U: InsideFunc<T>>(
        self,
        constraint: U,
        err: impl ToString
    ) -> Self;
fn toggle_msg_repeat(self) -> Self;
fn prompting_on(self, prompt_output: RefCell<Box<dyn Write>>) -> Self;
fn prompting_on_stderr(self) -> Self; }
Expand description

Trait implemented by InputBuilder and InputBuilderOnce to standardize input settings.

Required methods

Changes or adds a prompt message that gets printed once when input if fetched.

Custom messages are written on the same line as the input cursor.

let username: String = input().msg("Please input your name: ").get();

If you wish to fetch input from the next line append a \n.

let username: String = input().msg("Please input your name:\n").get();

Changes or adds a prompt message and that is repeated each time input is requested.

let username: String = input().repeat_msg("Please input your name: ").get();

Changes fallback error message.

The default error message is “That value does not pass. Please try again”.

let input = input::<u32>()
    .msg("Please input a positive number: ")
    .err("That does not look like a positive number. Please try again")
    .get();

Adds a validation check on input to ensure the value meets your criteria.

If you want an integer that is not 6 you could write.

let input = input().add_test(|x: &u8| *x != 6).get();

However for this example it would be better to use InputConstraints::not

Does the same thing as InputBuild::err, but with a custom error message printed when the test fails.

If you want a value from 4 to 9 that is not 6 you could write.

let input = input()
    .msg("Please input a number from 4 to 9 that is not 6: ")
    .inside_err(
        4..=9,
        "That does not look like a number from 4 to 9. Please try again"
    )
    .add_err_test(
        |x| *x != 6,
        "That value is 6! I don't want 6. Please try again"
    )
    .err("That does not look like a number. Please try again")
    .get();

Used specify custom error messages that depend on the errors produced by FromStr.

You can specify custom error messages that depend on the errors produced by FromStr with InputBuild::err_match().

Here is an extract from the point_input example showing this in practice.

let point = input::<Point>()
    .repeat_msg("Please input a point in 2D space in the format (x, y): ")
    .err_match(|e| {
        Some(match e {
            ParsePointError::FailedParse(s) => format!(
                "Failed to parse \"{}\" it is not a number that can be parsed.",
                s
            ),
            ParsePointError::Not2Dimensional(num) => {
                format!("What you inputted was {} dimensional.", num)
            }
            ParsePointError::NonNumeric => "That contains a invalid character.".to_string(),
        })
    })
    .get();

In nightly rust this can also be done with integers with the feature flag #![feature(int_error_matching)] shown in the example match_num_err.

use core::num::IntErrorKind::*;
let input = input::<i16>()
    .err_match(|x| {
        Some(
            match x.kind() {
                Empty => "You did not input any value. Try again.",
                InvalidDigit => "You typed an invalid digit. Try again using only numbers.",
                Overflow => "Integer is too large to store. Try again with a smaller number.",
                Underflow => "Integer is too small to store. Try again with a smaller number.",
                _ => "That value did not pass for an unexpected reason.",
            }
            .to_string(),
        )
    })
    .repeat_msg("Please input a number: ")
    .get();

Ensures that input is within a range, array or vector.

If you want an integer from 4 to 9 you could write.

let input = input().inside([4, 5, 6, 7, 8, 9]).get();

or alternatively

let input = input().inside(4..=9).get();

Does the same thing as InputBuild::inside, but with a custom error message printed when input fails.

Toggles whether a prompt message gets printed once or each time input is requested.

Send prompts to custom writer instead of stdout

Send prompts to stderr instead of stdout

Implementors