pub struct QuestionBuilder<T, R, W> { /* private fields */ }Expand description
Async I/O handler (in builder form).
§Contents
- Processing
- Constructor
- Message
- Testing value
- Testing value extended
- Useful Settings
- Prompt functionalities
- Processing Text Input
- Advanced Methods
§Processing
There are many steps in handling input and giving feedback, and therefore many options. As a preview, these are the main steps:
- Write message
- Take input
- Test the input as a
String - Parse input
- Test the value
- Give feedback
- Return the value
For more details, checkout the ask method.
Implementations§
Source§impl<T, R, W> QuestionBuilder<T, R, W>
§Constructor
impl<T, R, W> QuestionBuilder<T, R, W>
§Constructor
Sourcepub fn new<F, E>(reader: R, writer: W, parser: F) -> Self
pub fn new<F, E>(reader: R, writer: W, parser: F) -> Self
Constructs a new QuestionBuilder<T, R, W>.
§Remarks
There are two default behaviours while handling strings.
- The
preparsertrims the end of the input. - The
error_formatteradds a new line before display.
Source§impl<T, R, W> QuestionBuilder<T, R, W>
impl<T, R, W> QuestionBuilder<T, R, W>
Sourcepub fn new_fromstr(reader: R, writer: W) -> Self
pub fn new_fromstr(reader: R, writer: W) -> Self
Constructs a new QuestionBuilder<T, R, W> where
the parser is given by the implementation of FromStr.
Source§impl<T, R, W> QuestionBuilder<T, R, W>
§Message
Main messages that will be displayed.
impl<T, R, W> QuestionBuilder<T, R, W>
§Message
Main messages that will be displayed.
Sourcepub fn repeat_message(self, message: impl ToString) -> Self
pub fn repeat_message(self, message: impl ToString) -> Self
Message to be displayed repeatedly before each attempt.
Sourcepub fn help(self, help: impl ToString) -> Self
pub fn help(self, help: impl ToString) -> Self
Help message to be displayed after the first failed attempt.
Sourcepub fn repeat_help(self, help: impl ToString) -> Self
pub fn repeat_help(self, help: impl ToString) -> Self
Help message to be displayed every time an attempt failed.
Source§impl<T, R, W> QuestionBuilder<T, R, W>
§Testing value
We encourage to write tests whose error should be displayed to the user,
and add them with the test_with_msg method.
impl<T, R, W> QuestionBuilder<T, R, W>
§Testing value
We encourage to write tests whose error should be displayed to the user,
and add them with the test_with_msg method.
Source§impl<T, R, W> QuestionBuilder<T, R, W>
impl<T, R, W> QuestionBuilder<T, R, W>
Sourcepub fn inside<I>(self, iterator: I) -> Selfwhere
I: IntoIterator<Item = T> + 'static,
pub fn inside<I>(self, iterator: I) -> Selfwhere
I: IntoIterator<Item = T> + 'static,
Test if the value is inside an iterator.
§Remarks
To prevent infinite loops, make sure iterator is finite.
Also, there is a default message you might want to change.
Sourcepub fn inside_with_msg<I, M>(self, iterator: I, message: M) -> Self
pub fn inside_with_msg<I, M>(self, iterator: I, message: M) -> Self
Test if the value is inside an iterator, displaying a message upon failure.
§Remarks
To prevent infinite loops, make sure iterator is finite.
Sourcepub fn max(self, upper_bound: T) -> Self
pub fn max(self, upper_bound: T) -> Self
Test if the value is at most upper_bound.
§Remarks
There is a default message you might want to change.
Sourcepub fn max_with_msg<M>(self, upper_bound: T, message: M) -> Self
pub fn max_with_msg<M>(self, upper_bound: T, message: M) -> Self
Test if the value is at most upper_bound, displaying a message upon failure.
Sourcepub fn min_max(self, lower_bound: T, upper_bound: T) -> Self
pub fn min_max(self, lower_bound: T, upper_bound: T) -> Self
Test if the value is between lower_bound and upper_bound, including borders.
§Remarks
There is a default message you might want to change.
Sourcepub fn min_max_with_msg<M>(
self,
lower_bound: T,
upper_bound: T,
message: M,
) -> Self
pub fn min_max_with_msg<M>( self, lower_bound: T, upper_bound: T, message: M, ) -> Self
Test if the value is between lower_bound and upper_bound, including borders,
displaying a message upon failure.
Sourcepub fn min(self, lower_bound: T) -> Self
pub fn min(self, lower_bound: T) -> Self
Test if the value is at least lower_bound.
§Remarks
There is a default message you might want to change.
Sourcepub fn min_with_msg<M>(self, lower_bound: T, message: M) -> Self
pub fn min_with_msg<M>(self, lower_bound: T, message: M) -> Self
Test if the value is at least lower_bound, displaying a message upon failure.
Sourcepub fn not_with_msg<M>(self, other: T, message: M) -> Self
pub fn not_with_msg<M>(self, other: T, message: M) -> Self
Test if the value is not other, displaying a message upon failure.
Source§impl<T, R, W> QuestionBuilder<T, R, W>
§Useful settings
impl<T, R, W> QuestionBuilder<T, R, W>
§Useful settings
Sourcepub fn attempts<O>(self, attempts: O) -> Self
pub fn attempts<O>(self, attempts: O) -> Self
Bound the number of possible attempts.
The default value is None, which gives infinite attempts to the user.
Sourcepub fn attempts_with_feedback<F>(self, attempts: usize, feedback: F) -> Self
pub fn attempts_with_feedback<F>(self, attempts: usize, feedback: F) -> Self
Bound the number of possible attempts, displaying a message before any input is read.
The default value is None, which gives infinite attempts to the user.
§Remarks
Before the first attempt, a message will be displayed, so make sure to handle that case.
Sourcepub fn default_value<S>(self, value: S) -> Self
pub fn default_value<S>(self, value: S) -> Self
Give a default value in case the input is not required and empty.
§Remarks
Default values are NOT tested, so make sure that it is a value that passes your tests!
Sourcepub fn required(self) -> Self
pub fn required(self) -> Self
Requires that the input is not empty to continue.
§Remarks
There is a default message you might want to change.
Sourcepub fn required_with_msg(self, message: impl ToString) -> Self
pub fn required_with_msg(self, message: impl ToString) -> Self
Requires that the input is not empty to continue, displaying a message upon failure.
Sourcepub fn required_toogle(self) -> Self
pub fn required_toogle(self) -> Self
Toggles between requiring and not requiring input.
Source§impl<T, R, W> QuestionBuilder<T, R, W>
§Executors
For ease of use, there are some built-in executors.
impl<T, R, W> QuestionBuilder<T, R, W>
§Executors
For ease of use, there are some built-in executors.
Sourcepub fn timeout(self, duration: Duration) -> Self
pub fn timeout(self, duration: Duration) -> Self
Set a maximum time for the user to finish answering the question.
§Remarks
This time corresponds to the whole execution, including displaying feedback. So the user might enter valid input before the time runs out, but the whole process might still timeout.
Source§impl<T, R, W> QuestionBuilder<T, R, W>
§Prompt functionalities
impl<T, R, W> QuestionBuilder<T, R, W>
§Prompt functionalities
Sourcepub async fn ask(self) -> Result<T, Processing>
pub async fn ask(self) -> Result<T, Processing>
Asynchronously gets input from the user.
The detailed process is as follows.
- Check there are
attemptsleft - Write
message - Write
feedback from attempts - Read input
- Apply the
preparserto the input - Return
default_valueif it corresponds - Apply all
str_tests - Convert the input with
parser - Apply all
tests - Write
feedback - Return the value
§Remarks
There are two types of messages that can be displayed during the process:
feedback and error messages.
Feedback is displayed as given by the corresponding method.
Error messages are displayed after applying error_formatter.
More over, error messages are displayed through the eyre crate.
Therefore, you can further configure their display
(check out EyreHandler). error_formatter
is applied on top of displaying the error.
Sourcepub fn ask_and_wait(self) -> Result<T, Processing>
pub fn ask_and_wait(self) -> Result<T, Processing>
Synchronously gets input from the user.
Convenience method for async_std::task::block_on(self.ask()).
§Remarks
Although this method goes against the goal of this crate, it is useful for quick implementations.
Source§impl<T, R, W> QuestionBuilder<T, R, W>
impl<T, R, W> QuestionBuilder<T, R, W>
Sourcepub fn preparser<F>(self, preparser: F) -> Self
pub fn preparser<F>(self, preparser: F) -> Self
Set the preparser for the input.
This is applied to the raw input before being parsed to T.
In CLI applications, it is useful to clean the leading new line that comes with the input.
Sourcepub fn str_test<F>(self, str_test: F) -> Self
pub fn str_test<F>(self, str_test: F) -> Self
Add a test over the unparsed input.
§Remarks
There is a default message that you might want to change.
Sourcepub fn str_test_with_msg<F, M>(self, str_test: F, message: M) -> Self
pub fn str_test_with_msg<F, M>(self, str_test: F, message: M) -> Self
Add a test over the unparsed input, displaying a message upon failure.
Sourcepub fn length(self, exact_length: usize) -> Self
pub fn length(self, exact_length: usize) -> Self
Tests that the input length is equal to exact_length.
§Remarks
There is a default message that you might want to change.
Sourcepub fn length_with_msg<M>(self, exact_length: usize, message: M) -> Self
pub fn length_with_msg<M>(self, exact_length: usize, message: M) -> Self
Tests that the input length is equal to exact_length, displaying a message upon failure.
Sourcepub fn max_length(self, max_length: usize) -> Self
pub fn max_length(self, max_length: usize) -> Self
Tests that the input length is less or equal to max_length.
§Remarks
There is a default message that you might want to change.
Sourcepub fn max_length_with_msg<M>(self, max_length: usize, message: M) -> Self
pub fn max_length_with_msg<M>(self, max_length: usize, message: M) -> Self
Tests that the input length is less or equal to max_length, displaying a message upon failure.
Sourcepub fn min_length(self, min_length: usize) -> Self
pub fn min_length(self, min_length: usize) -> Self
Tests that the input length is greater or equal to min_length.
§Remarks
There is a default message that you might want to change.
Sourcepub fn min_length_with_msg<M>(self, min_length: usize, message: M) -> Self
pub fn min_length_with_msg<M>(self, min_length: usize, message: M) -> Self
Tests that the input length is greater or equal to min_length, displaying a message upon failure.
Source§impl<T, R, W> QuestionBuilder<T, R, W>
§Advanced methods
impl<T, R, W> QuestionBuilder<T, R, W>
§Advanced methods
Sourcepub fn reader<R2: Read>(self, other_reader: R2) -> QuestionBuilder<T, R2, W>
pub fn reader<R2: Read>(self, other_reader: R2) -> QuestionBuilder<T, R2, W>
Change the reader.
Sourcepub fn writer<W2: Write>(self, other_writer: W2) -> QuestionBuilder<T, R, W2>
pub fn writer<W2: Write>(self, other_writer: W2) -> QuestionBuilder<T, R, W2>
Change the writer.
Sourcepub fn error_formatter<F>(self, error_formatter: F) -> Self
pub fn error_formatter<F>(self, error_formatter: F) -> Self
Change the way errors are displayed.
§Remarks
This can be useful if to:
- Change the default behaviour of appending a newline.
- Not display a message upon an error.
§Examples
Silent errors. Now, only messages will be displayed.
let _num = asking::question()
.message("Give me a number bigger than 3, please. I will not tell you anything else.\n")
.test(|i: &u32| *i > 3)
.error_formatter(|_| "".to_string())
.ask()
.await?;Sourcepub fn parser_feedback_toggle(self) -> Self
pub fn parser_feedback_toggle(self) -> Self
Toggle the feedback from the parser.
If activated, errors from parsing will be displayed.
Sourcepub fn parser_with_feedback<F>(self, parser: F) -> Self
pub fn parser_with_feedback<F>(self, parser: F) -> Self
Set the parser for the input.
Errors will be displayed if they occur.
Sourcepub fn str_test_with_feedback<F>(self, str_test: F) -> Self
pub fn str_test_with_feedback<F>(self, str_test: F) -> Self
Add a test over the unparsed input.
Errors will be displayed if they occur.