IntBuilder

Struct IntBuilder 

Source
pub struct IntBuilder<'a> { /* private fields */ }
Expand description

The builder for an int prompt.

The number is parsed using from_str.

See the various methods for more details on each available option.

§Examples

use requestty::Question;

let int = Question::int("age")
    .message("What is your age?")
    .validate(|age, previous_answers| {
        if age > 0 && age < 130 {
            Ok(())
        } else {
            Err(format!("You cannot be {} years old!", age))
        }
    })
    .build();

Implementations§

Source§

impl<'a> IntBuilder<'a>

Source

pub fn message<M>(self, message: M) -> Self
where M: Into<Getter<'a, String>>,

The message to display when the prompt is rendered in the terminal.

It can be either a String or a FnOnce that returns a String. If it is a function, it is passed all the previous Answers, and will be called right before the question is prompted to the user.

If it is not given, the message defaults to “<name>: “.

§Examples
use requestty::Question;

let int = Question::int("int")
    .message("Please enter a number")
    .build();
Source

pub fn when<W>(self, when: W) -> Self
where W: Into<Getter<'a, bool>>,

Whether to ask the question (true) or not (false).

It can be either a bool or a FnOnce that returns a bool. If it is a function, it is passed all the previous Answers, and will be called right before the question is prompted to the user.

If it is not given, it defaults to true.

§Examples
use requestty::{Question, Answers};

let int = Question::int("int")
    .when(|previous_answers: &Answers| match previous_answers.get("ask_number") {
        Some(ans) => ans.as_bool().unwrap(),
        None => true,
    })
    .build();
Source

pub fn ask_if_answered(self, ask_if_answered: bool) -> Self

Prompt the question even if it is answered.

By default if an answer with the given name already exists, the question will be skipped. This can be overridden by setting ask_if_answered is set to true.

If this is not given, it defaults to false.

If you need to dynamically decide whether the question should be asked, use when.

§Examples
use requestty::{Question, Answers};

let int = Question::int("int")
    .ask_if_answered(true)
    .build();
Source

pub fn on_esc<T>(self, on_esc: T) -> Self
where T: Into<Getter<'a, OnEsc>>,

Configure what to do when the user presses the Esc key.

It can be either a OnEsc or a FnOnce that returns a OnEsc. If it is a function, it is passed all the previous Answers, and will be called right before the question is prompted to the user.

If it is not given, it defaults to OnEsc::Ignore.

§Examples
use requestty::{Question, Answers, OnEsc};

let int = Question::int("int")
    .on_esc(OnEsc::Terminate)
    .build();
Source

pub fn default(self, default: i64) -> Self

Set a default value

If the input text is empty, the default is taken as the answer.

If default is used, validation is skipped, but filter is still called.

§Examples
use requestty::Question;

let int = Question::int("int")
    .default(10)
    .build();
Source

pub fn filter<F>(self, filter: F) -> Self
where F: FnOnce(i64, &Answers) -> i64 + 'a,

Function to change the final submitted value before it is displayed to the user and added to the Answers.

It is a FnOnce that is given the answer and the previous Answers, and should return the new answer.

This will be called after the answer has been validated.

§Examples
use requestty::Question;

let int = Question::int("int")
   .filter(|n, previous_answers| n + 10)
    .build();
Source

pub fn validate<F>(self, filter: F) -> Self
where F: FnMut(i64, &Answers) -> Result<(), String> + 'a,

Function to validate the submitted value before it’s returned.

It is a FnMut that is given the answer and the previous Answers, and should return Ok(()) if the given answer is valid. If it is invalid, it should return an Err with the error message to display to the user.

This will be called when the user presses the Enter key.

§Examples
use requestty::Question;

let int = Question::int("int")
    .validate(|n, previous_answers| {
       if n.is_positive() {
            Ok(())
        } else {
            Err("Please enter a positive number".to_owned())
        }
    })
    .build();
Source

pub fn validate_on_key<F>(self, filter: F) -> Self
where F: FnMut(i64, &Answers) -> bool + 'a,

Function to validate the value on every key press. If the validation fails, the text is displayed in red.

It is a FnMut that is given the answer and the previous Answers, and should return true if it is valid.

This will be called after every change in state. Note that this validation is purely cosmetic. If the user presses Enter, this function is not called. Instead, the one supplied to validate (if any) is the only validation that can prevent a user submission. This is required since final validation needs to return an error message to show the user.

Note, the input will be showed in red if the number cannot be parsed even if this function is not supplied.

§Examples
use requestty::Question;

let int = Question::int("int")
    .validate_on_key(|n, previous_answers| n.is_positive())
    // Still required as this is the final validation and validate_on_key is purely cosmetic
    .validate(|n, previous_answers| {
       if n.is_positive() {
            Ok(())
        } else {
            Err("Please enter a positive number".to_owned())
        }
    })
    .build();
Source

pub fn transform<F>(self, transform: F) -> Self
where F: FnOnce(i64, &Answers, &mut dyn Backend) -> Result<()> + 'a,

Change the way the answer looks when displayed to the user.

It is a FnOnce that is given the answer, previous Answers and the Backend to display the answer on. After the transform is called, a new line is also added.

It will only be called once the user finishes answering the question.

§Examples
use requestty::Question;

let int = Question::int("int")
    .transform(|n, previous_answers, backend| {
        write!(backend, "{:e}", n)
    })
    .build();
Source

pub fn build(self) -> Question<'a>

Consumes the builder returning a Question

Trait Implementations§

Source§

impl<'a> Debug for IntBuilder<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> From<IntBuilder<'a>> for Question<'a>

Source§

fn from(builder: IntBuilder<'a>) -> Self

Consumes the builder returning a Question

Auto Trait Implementations§

§

impl<'a> Freeze for IntBuilder<'a>

§

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

§

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

§

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

§

impl<'a> Unpin for IntBuilder<'a>

§

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

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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 T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.