mod action;
mod config;
mod prompt;
#[cfg(test)]
#[cfg(feature = "crossterm")]
mod test;
pub use action::*;
use crate::{
config::get_configuration,
error::{InquireError, InquireResult},
formatter::StringFormatter,
prompts::prompt::Prompt,
terminal::get_default_terminal,
ui::{Backend, PasswordBackend, RenderConfig},
validator::StringValidator,
};
use self::prompt::PasswordPrompt;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum PasswordDisplayMode {
Hidden,
Masked,
Full,
}
#[derive(Clone)]
pub struct Password<'a> {
pub message: &'a str,
pub custom_confirmation_message: Option<&'a str>,
pub custom_confirmation_error_message: Option<&'a str>,
pub help_message: Option<&'a str>,
pub formatter: StringFormatter<'a>,
pub display_mode: PasswordDisplayMode,
pub enable_display_toggle: bool,
pub enable_confirmation: bool,
pub validators: Vec<Box<dyn StringValidator>>,
pub render_config: RenderConfig<'a>,
}
impl<'a> Password<'a> {
pub const DEFAULT_FORMATTER: StringFormatter<'a> = &|_| String::from("********");
pub const DEFAULT_VALIDATORS: Vec<Box<dyn StringValidator>> = vec![];
pub const DEFAULT_HELP_MESSAGE: Option<&'a str> = None;
pub const DEFAULT_ENABLE_DISPLAY_TOGGLE: bool = false;
pub const DEFAULT_ENABLE_CONFIRMATION: bool = true;
pub const DEFAULT_DISPLAY_MODE: PasswordDisplayMode = PasswordDisplayMode::Hidden;
pub fn new(message: &'a str) -> Self {
Self {
message,
custom_confirmation_message: None,
custom_confirmation_error_message: None,
enable_confirmation: Self::DEFAULT_ENABLE_CONFIRMATION,
enable_display_toggle: Self::DEFAULT_ENABLE_DISPLAY_TOGGLE,
display_mode: Self::DEFAULT_DISPLAY_MODE,
help_message: Self::DEFAULT_HELP_MESSAGE,
formatter: Self::DEFAULT_FORMATTER,
validators: Self::DEFAULT_VALIDATORS,
render_config: get_configuration(),
}
}
pub fn with_help_message(mut self, message: &'a str) -> Self {
self.help_message = Some(message);
self
}
pub fn with_display_toggle_enabled(mut self) -> Self {
self.enable_display_toggle = true;
self
}
pub fn without_confirmation(mut self) -> Self {
self.enable_confirmation = false;
self
}
pub fn with_custom_confirmation_message(mut self, message: &'a str) -> Self {
self.custom_confirmation_message.replace(message);
self
}
pub fn with_custom_confirmation_error_message(mut self, message: &'a str) -> Self {
self.custom_confirmation_error_message.replace(message);
self
}
pub fn with_display_mode(mut self, mode: PasswordDisplayMode) -> Self {
self.display_mode = mode;
self
}
pub fn with_formatter(mut self, formatter: StringFormatter<'a>) -> Self {
self.formatter = formatter;
self
}
pub fn with_validator<V>(mut self, validator: V) -> Self
where
V: StringValidator + 'static,
{
self.validators.push(Box::new(validator));
self
}
pub fn with_validators(mut self, validators: &[Box<dyn StringValidator>]) -> Self {
for validator in validators {
#[allow(suspicious_double_ref_op)]
self.validators.push(validator.clone());
}
self
}
pub fn with_render_config(mut self, render_config: RenderConfig<'a>) -> Self {
self.render_config = render_config;
self
}
pub fn prompt_skippable(self) -> InquireResult<Option<String>> {
match self.prompt() {
Ok(answer) => Ok(Some(answer)),
Err(InquireError::OperationCanceled) => Ok(None),
Err(err) => Err(err),
}
}
pub fn prompt(self) -> InquireResult<String> {
let (input_reader, terminal) = get_default_terminal()?;
let mut backend = Backend::new(input_reader, terminal, self.render_config)?;
self.prompt_with_backend(&mut backend)
}
pub(crate) fn prompt_with_backend<B: PasswordBackend>(
self,
backend: &mut B,
) -> InquireResult<String> {
PasswordPrompt::from(self).prompt(backend)
}
}