Struct inquire::validator::MaxLengthValidator
source · pub struct MaxLengthValidator { /* private fields */ }
Expand description
Built-in validator that checks whether the answer length is smaller than or equal to the specified threshold.
The validator uses a custom-built length function that has a special implementation for strings which counts the number of graphemes. See this StackOverflow question.
§Examples
use inquire::validator::{MaxLengthValidator, StringValidator, Validation};
let validator = MaxLengthValidator::new(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 = MaxLengthValidator::new(5).with_message("Not too large!");
assert_eq!(Validation::Valid, validator.validate("Good")?);
assert_eq!(Validation::Invalid("Not too large!".into()), validator.validate("Terrible")?);
Implementations§
source§impl MaxLengthValidator
impl MaxLengthValidator
sourcepub fn new(limit: usize) -> Self
pub fn new(limit: usize) -> Self
Create a new instance of this validator, requiring at most the given length, otherwise returning an error with default message.
sourcepub fn with_message(self, message: impl Into<String>) -> Self
pub fn with_message(self, message: impl Into<String>) -> Self
Define a custom error message returned by the validator.
Defaults to The length of the response should be at most $length
.
Trait Implementations§
source§impl Clone for MaxLengthValidator
impl Clone for MaxLengthValidator
source§fn clone(&self) -> MaxLengthValidator
fn clone(&self) -> MaxLengthValidator
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl<T: ?Sized> MultiOptionValidator<T> for MaxLengthValidator
impl<T: ?Sized> MultiOptionValidator<T> for MaxLengthValidator
source§fn validate(
&self,
input: &[ListOption<&T>]
) -> Result<Validation, CustomUserError>
fn validate( &self, input: &[ListOption<&T>] ) -> Result<Validation, CustomUserError>
Confirm the given input list is a valid value.
source§impl StringValidator for MaxLengthValidator
impl StringValidator for MaxLengthValidator
source§fn validate(&self, input: &str) -> Result<Validation, CustomUserError>
fn validate(&self, input: &str) -> Result<Validation, CustomUserError>
Confirm the given input string is a valid value.
Auto Trait Implementations§
impl Freeze for MaxLengthValidator
impl RefUnwindSafe for MaxLengthValidator
impl Send for MaxLengthValidator
impl Sync for MaxLengthValidator
impl Unpin for MaxLengthValidator
impl UnwindSafe for MaxLengthValidator
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more