dialoguer/
validate.rs

1//! Provides validation for text inputs
2
3/// Trait for input validators.
4///
5/// A generic implementation for `Fn(&str) -> Result<(), E>` is provided
6/// to facilitate development.
7pub trait InputValidator<T> {
8    type Err;
9
10    /// Invoked with the value to validate.
11    ///
12    /// If this produces `Ok(())` then the value is used and parsed, if
13    /// an error is returned validation fails with that error.
14    fn validate(&mut self, input: &T) -> Result<(), Self::Err>;
15}
16
17impl<T, F, E> InputValidator<T> for F
18where
19    F: FnMut(&T) -> Result<(), E>,
20{
21    type Err = E;
22
23    fn validate(&mut self, input: &T) -> Result<(), Self::Err> {
24        self(input)
25    }
26}
27
28/// Trait for password validators.
29#[allow(clippy::ptr_arg)]
30pub trait PasswordValidator {
31    type Err;
32
33    /// Invoked with the value to validate.
34    ///
35    /// If this produces `Ok(())` then the value is used and parsed, if
36    /// an error is returned validation fails with that error.
37    fn validate(&self, input: &String) -> Result<(), Self::Err>;
38}
39
40impl<F, E> PasswordValidator for F
41where
42    F: Fn(&String) -> Result<(), E>,
43{
44    type Err = E;
45
46    fn validate(&self, input: &String) -> Result<(), Self::Err> {
47        self(input)
48    }
49}