Macro inquire::min_length[][src]

macro_rules! min_length {
    ($length : expr) => { ... };
    ($length : expr, $message : expr) => { ... };
}
Expand description

Built-in validator that checks whether the answer length is larger than or equal to the specified threshold.

When using this macro, you must also import the InquireLength trait. The validator uses a custom-built length function that has a special implementation for strings, as we can’t rely on a generic .len() for them. See this StackOverflow question.

Arguments

  • $length - Minimum length of the input.
  • $message - optional - Error message returned by the validator. Defaults to “The length of the response should be at least $length”

Examples

use inquire::{min_length, validator::{InquireLength, StringValidator}};

let validator: StringValidator = min_length!(3);
assert_eq!(Ok(()), validator("Yes"));
assert_eq!(Err(String::from("The length of the response should be at least 3")), validator("No"));

let validator: StringValidator = min_length!(3, "You have to give me more than that!");
assert_eq!(Ok(()), validator("Yes"));
assert_eq!(Err(String::from("You have to give me more than that!")), validator("No"));