2024-01-16 02:44:48 +00:00
|
|
|
//! Prompt display and interaction management.
|
|
|
|
|
2024-01-11 04:32:26 +00:00
|
|
|
use std::borrow::Borrow;
|
2023-12-21 17:04:35 +00:00
|
|
|
|
2023-12-22 19:39:25 +00:00
|
|
|
#[cfg(feature = "mnemonic")]
|
|
|
|
use keyfork_mnemonic_util::Wordlist;
|
|
|
|
|
2024-01-16 02:44:48 +00:00
|
|
|
///
|
2024-01-11 04:32:26 +00:00
|
|
|
pub mod terminal;
|
2024-01-09 07:21:46 +00:00
|
|
|
pub mod validators;
|
2024-01-11 04:32:26 +00:00
|
|
|
pub use terminal::{Terminal, DefaultTerminal, default_terminal};
|
2024-01-09 07:21:46 +00:00
|
|
|
|
2024-01-16 02:44:48 +00:00
|
|
|
/// An error occurred while displaying a prompt.
|
2023-12-21 17:04:35 +00:00
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum Error {
|
2024-01-16 02:44:48 +00:00
|
|
|
/// The given handler is not a TTY and can't be used to display prompts.
|
2023-12-21 17:04:35 +00:00
|
|
|
#[error("The given handler is not a TTY")]
|
|
|
|
NotATTY,
|
|
|
|
|
2024-01-16 02:44:48 +00:00
|
|
|
/// Validating user input failed.
|
2024-01-09 07:21:46 +00:00
|
|
|
#[error("Validation of the input failed after {0} retries (last error: {1})")]
|
|
|
|
Validation(u8, String),
|
|
|
|
|
2024-01-18 19:46:31 +00:00
|
|
|
/// A ctrl-c interrupt was caught by the handler.
|
|
|
|
#[error("User pressed ctrl-c, terminating the session")]
|
|
|
|
CtrlC,
|
|
|
|
|
2024-01-16 02:44:48 +00:00
|
|
|
/// An error occurred while interacting with a terminal.
|
2023-12-21 17:04:35 +00:00
|
|
|
#[error("IO Error: {0}")]
|
|
|
|
IO(#[from] std::io::Error),
|
|
|
|
}
|
|
|
|
|
2024-01-16 02:44:48 +00:00
|
|
|
#[allow(missing_docs)]
|
2023-12-21 17:04:35 +00:00
|
|
|
pub type Result<T, E = Error> = std::result::Result<T, E>;
|
|
|
|
|
2024-01-16 02:44:48 +00:00
|
|
|
/// A message displayed by [`PromptHandler::prompt_message`].
|
2023-12-28 22:54:38 +00:00
|
|
|
pub enum Message {
|
2024-01-16 02:44:48 +00:00
|
|
|
/// A textual message, wrapping at space boundaries when reaching the end of the terminal.
|
2023-12-28 22:54:38 +00:00
|
|
|
Text(String),
|
2024-01-16 02:44:48 +00:00
|
|
|
/// A data message, with no word wrapping, and automatic hiding of the message when a terminal
|
|
|
|
/// is too small.
|
2023-12-28 22:54:38 +00:00
|
|
|
Data(String),
|
|
|
|
}
|
|
|
|
|
2024-01-16 02:44:48 +00:00
|
|
|
/// A trait to allow displaying prompts and accepting input.
|
2024-01-11 04:28:56 +00:00
|
|
|
pub trait PromptHandler {
|
2024-01-16 02:44:48 +00:00
|
|
|
/// Prompt the user for input.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// The method may return an error if the message was not able to be displayed or if the input
|
|
|
|
/// could not be read.
|
2024-01-11 04:28:56 +00:00
|
|
|
fn prompt_input(&mut self, prompt: &str) -> Result<String>;
|
|
|
|
|
2024-01-16 02:44:48 +00:00
|
|
|
/// Prompt the user for input based on a wordlist.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// The method may return an error if the message was not able to be displayed or if the input
|
|
|
|
/// could not be read.
|
|
|
|
#[cfg(feature = "mnemonic")]
|
2024-01-11 04:28:56 +00:00
|
|
|
fn prompt_wordlist(&mut self, prompt: &str, wordlist: &Wordlist) -> Result<String>;
|
|
|
|
|
2024-01-16 02:44:48 +00:00
|
|
|
/// Prompt the user for input based on a wordlist, while validating the wordlist using a
|
|
|
|
/// provided parser function, returning the type from the parser.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// The method may return an error if the message was not able to be displayed, if the input
|
|
|
|
/// could not be read, or if the parser returned an error.
|
2024-01-11 04:28:56 +00:00
|
|
|
#[cfg(feature = "mnemonic")]
|
|
|
|
fn prompt_validated_wordlist<V, F, E>(
|
|
|
|
&mut self,
|
|
|
|
prompt: &str,
|
|
|
|
wordlist: &Wordlist,
|
|
|
|
retries: u8,
|
|
|
|
validator_fn: F,
|
|
|
|
) -> Result<V, Error>
|
|
|
|
where
|
|
|
|
F: Fn(String) -> Result<V, E>,
|
|
|
|
E: std::error::Error;
|
|
|
|
|
2024-01-16 02:44:48 +00:00
|
|
|
/// Prompt the user for a passphrase, which is hidden while typing.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// The method may return an error if the message was not able to be displayed or if the input
|
|
|
|
/// could not be read.
|
2024-01-11 04:28:56 +00:00
|
|
|
fn prompt_passphrase(&mut self, prompt: &str) -> Result<String>;
|
|
|
|
|
2024-01-16 02:44:48 +00:00
|
|
|
/// Prompt the user for a passphrase, which is hidden while typing, and validate the passphrase
|
|
|
|
/// using a provided parser function, returning the type from the parser.
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// The method may return an error if the message was not able to be displayed, if the input
|
|
|
|
/// could not be read, or if the parser returned an error.
|
2024-01-11 04:28:56 +00:00
|
|
|
fn prompt_validated_passphrase<V, F, E>(
|
|
|
|
&mut self,
|
|
|
|
prompt: &str,
|
|
|
|
retries: u8,
|
|
|
|
validator_fn: F,
|
|
|
|
) -> Result<V, Error>
|
|
|
|
where
|
|
|
|
F: Fn(String) -> Result<V, E>,
|
|
|
|
E: std::error::Error;
|
|
|
|
|
2024-01-16 02:44:48 +00:00
|
|
|
/// Prompt the user with a [`Message`].
|
|
|
|
///
|
|
|
|
/// # Errors
|
|
|
|
/// The method may return an error if the message was not able to be displayed or if an error
|
|
|
|
/// occurred while waiting for the user to dismiss the message.
|
2024-01-11 04:28:56 +00:00
|
|
|
fn prompt_message(&mut self, prompt: impl Borrow<Message>) -> Result<()>;
|
|
|
|
}
|