Macro inquire::max_length
source · macro_rules! max_length { ($length:expr) => { ... }; ($length:expr, $message:expr) => { ... }; }
Expand description
Shorthand for the built-in MaxLengthValidator
that checks whether the answer length is
smaller than or equal to the specified threshold.
§Arguments
$length
- Maximum length of the input.$message
- optional - Error message returned by the validator. Defaults to “The length of the response should be at most $length”
§Examples
use inquire::{max_length, validator::{StringValidator, Validation}};
let validator = max_length!(5);
assert_eq!(Validation::Valid, validator.validate("Good")?);
assert_eq!(Validation::Invalid("The length of the response should be at most 5".into()), validator.validate("Terrible")?);
let validator = max_length!(5, "Not too large!");
assert_eq!(Validation::Valid, validator.validate("Good")?);
assert_eq!(Validation::Invalid("Not too large!".into()), validator.validate("Terrible")?);