Struct inquire::validator::ExactLengthValidator
source · pub struct ExactLengthValidator { /* private fields */ }
Expand description
Built-in validator that checks whether the answer length is equal to the specified value.
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::{ExactLengthValidator, StringValidator, Validation};
let validator = ExactLengthValidator::new(3);
assert_eq!(Validation::Valid, validator.validate("Yes")?);
assert_eq!(
Validation::Invalid("The length of the response should be 3".into()),
validator.validate("No")?,
);
let validator = ExactLengthValidator::new(3).with_message("Three characters please.");
assert_eq!(Validation::Valid, validator.validate("Yes")?);
assert_eq!(Validation::Invalid("Three characters please.".into()), validator.validate("No")?);
Implementations§
source§impl ExactLengthValidator
impl ExactLengthValidator
sourcepub fn new(length: usize) -> Self
pub fn new(length: usize) -> Self
Create a new instance of this validator, requiring exactly 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 $length
.
Trait Implementations§
source§impl Clone for ExactLengthValidator
impl Clone for ExactLengthValidator
source§fn clone(&self) -> ExactLengthValidator
fn clone(&self) -> ExactLengthValidator
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 ExactLengthValidator
impl<T: ?Sized> MultiOptionValidator<T> for ExactLengthValidator
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 ExactLengthValidator
impl StringValidator for ExactLengthValidator
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 ExactLengthValidator
impl RefUnwindSafe for ExactLengthValidator
impl Send for ExactLengthValidator
impl Sync for ExactLengthValidator
impl Unpin for ExactLengthValidator
impl UnwindSafe for ExactLengthValidator
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