From db990962691430f82012e52c3da01a1767bef370 Mon Sep 17 00:00:00 2001 From: vallentin Date: Tue, 4 Feb 2020 00:14:45 +0100 Subject: [PATCH 1/2] Added simple validate example --- examples/validate_input.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 examples/validate_input.rs diff --git a/examples/validate_input.rs b/examples/validate_input.rs new file mode 100644 index 00000000..01df4ee8 --- /dev/null +++ b/examples/validate_input.rs @@ -0,0 +1,19 @@ +extern crate dialoguer; + +use dialoguer::Input; + +fn main() { + let mail: String = Input::new() + .with_prompt("Enter email") + .validate_with(|input: &str| -> Result<(), &str> { + if input.contains('@') { + Ok(()) + } else { + Err("This is not a mail address") + } + }) + .interact() + .unwrap(); + + println!("Email: {}", mail); +} From 340b46b93642918154bce3320c803a865a80f1e8 Mon Sep 17 00:00:00 2001 From: vallentin Date: Tue, 4 Feb 2020 00:17:30 +0100 Subject: [PATCH 2/2] Added validate example to docs --- src/prompts.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/prompts.rs b/src/prompts.rs index 0a37929d..1b6ee42f 100644 --- a/src/prompts.rs +++ b/src/prompts.rs @@ -222,6 +222,23 @@ where } /// Registers a validator. + /// + /// # Example + /// + /// ```no_run + /// # use dialoguer::Input; + /// let mail: String = Input::new() + /// .with_prompt("Enter email") + /// .validate_with(|input: &str| -> Result<(), &str> { + /// if input.contains('@') { + /// Ok(()) + /// } else { + /// Err("This is not a mail address") + /// } + /// }) + /// .interact() + /// .unwrap(); + /// ``` pub fn validate_with(&mut self, validator: V) -> &mut Input<'a, T> { let old_validator_func = self.validator.take(); self.validator = Some(Box::new(move |value: &str| -> Option {