Struct dialoguer::Password

source ·
pub struct Password<'a> { /* private fields */ }
Expand description

Renders a password input prompt.

Example usage

use dialoguer::Password;

let password = Password::new().with_prompt("New Password")
    .with_confirmation("Confirm password", "Passwords mismatching")
    .interact()?;
println!("Length of the password is: {}", password.len());

Implementations§

source§

impl Password<'static>

source

pub fn new() -> Password<'static>

Creates a password input prompt.

source§

impl<'a> Password<'a>

source

pub fn with_prompt<S: Into<String>>(&mut self, prompt: S) -> &mut Self

Sets the password input prompt.

Examples found in repository?
examples/password.rs (line 5)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() {
    let password = Password::with_theme(&ColorfulTheme::default())
        .with_prompt("Password")
        .with_confirmation("Repeat password", "Error: the passwords don't match.")
        .validate_with(|input: &String| -> Result<(), &str> {
            if input.len() > 3 {
                Ok(())
            } else {
                Err("Password must be longer than 3")
            }
        })
        .interact()
        .unwrap();

    println!("Your password is {} characters long", password.len());
}
source

pub fn report(&mut self, val: bool) -> &mut Self

Indicates whether to report confirmation after interaction.

The default is to report.

source

pub fn with_confirmation<A, B>( &mut self, prompt: A, mismatch_err: B ) -> &mut Selfwhere A: Into<String>, B: Into<String>,

Enables confirmation prompting.

Examples found in repository?
examples/password.rs (line 6)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() {
    let password = Password::with_theme(&ColorfulTheme::default())
        .with_prompt("Password")
        .with_confirmation("Repeat password", "Error: the passwords don't match.")
        .validate_with(|input: &String| -> Result<(), &str> {
            if input.len() > 3 {
                Ok(())
            } else {
                Err("Password must be longer than 3")
            }
        })
        .interact()
        .unwrap();

    println!("Your password is {} characters long", password.len());
}
source

pub fn allow_empty_password(&mut self, allow_empty_password: bool) -> &mut Self

Allows/Disables empty password.

By default this setting is set to false (i.e. password is not empty).

source

pub fn validate_with<V>(&mut self, validator: V) -> &mut Selfwhere V: PasswordValidator + 'a, V::Err: ToString,

Registers a validator.

Example
let password: String = Password::new()
    .with_prompt("Enter password")
    .validate_with(|input: &String| -> Result<(), &str> {
        if input.len() > 8 {
            Ok(())
        } else {
            Err("Password must be longer than 8")
        }
    })
    .interact()
    .unwrap();
Examples found in repository?
examples/password.rs (lines 7-13)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() {
    let password = Password::with_theme(&ColorfulTheme::default())
        .with_prompt("Password")
        .with_confirmation("Repeat password", "Error: the passwords don't match.")
        .validate_with(|input: &String| -> Result<(), &str> {
            if input.len() > 3 {
                Ok(())
            } else {
                Err("Password must be longer than 3")
            }
        })
        .interact()
        .unwrap();

    println!("Your password is {} characters long", password.len());
}
source

pub fn interact(&self) -> Result<String>

Enables user interaction and returns the result.

If the user confirms the result is true, false otherwise. The dialog is rendered on stderr.

Examples found in repository?
examples/password.rs (line 14)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() {
    let password = Password::with_theme(&ColorfulTheme::default())
        .with_prompt("Password")
        .with_confirmation("Repeat password", "Error: the passwords don't match.")
        .validate_with(|input: &String| -> Result<(), &str> {
            if input.len() > 3 {
                Ok(())
            } else {
                Err("Password must be longer than 3")
            }
        })
        .interact()
        .unwrap();

    println!("Your password is {} characters long", password.len());
}
source

pub fn interact_on(&self, term: &Term) -> Result<String>

Like interact but allows a specific terminal to be set.

source§

impl<'a> Password<'a>

source

pub fn with_theme(theme: &'a dyn Theme) -> Self

Creates a password input prompt with a specific theme.

Examples found in repository?
examples/password.rs (line 4)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fn main() {
    let password = Password::with_theme(&ColorfulTheme::default())
        .with_prompt("Password")
        .with_confirmation("Repeat password", "Error: the passwords don't match.")
        .validate_with(|input: &String| -> Result<(), &str> {
            if input.len() > 3 {
                Ok(())
            } else {
                Err("Password must be longer than 3")
            }
        })
        .interact()
        .unwrap();

    println!("Your password is {} characters long", password.len());
}

Trait Implementations§

source§

impl Default for Password<'static>

source§

fn default() -> Password<'static>

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for Password<'a>

§

impl<'a> !Send for Password<'a>

§

impl<'a> !Sync for Password<'a>

§

impl<'a> Unpin for Password<'a>

§

impl<'a> !UnwindSafe for Password<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.