Trait conciliator::input::Input

source ·
pub trait Input {
    type T;

    // Required methods
    fn prompt(&self, buffer: &mut Buffer);
    fn validate(&self, user_input: &str) -> Option<Self::T>;

    // Provided method
    fn print<C: Conciliator + ?Sized>(&mut self, con: &C) { ... }
}
Expand description

Requesting user input

Used by Claw::input, where:

  • print is called once at the start of the input procedure to establish the context of the input being requested,
  • prompt is called to prompt the user for input,
  • then, a line is read from the standard input and trimmed before being passed to
  • validate, which (parses and) validates the input. If it returns
    • Some, the input procedure ends successfully.
    • None, the user will be prompted again (and again) until validate returns Some.

Required Associated Types§

source

type T

Type being requested from the user (after parsing)

Required Methods§

source

fn prompt(&self, buffer: &mut Buffer)

Make the request

Provide a concise question or request, ideally indicating the expected format. In general, do not add a trailing newline.

This will be called at least once, and then again for every time validate returns None.

For example: Are you sure you want to continue? [y/N]:

source

fn validate(&self, user_input: &str) -> Option<Self::T>

Parse & validate the user input

Returning Some(T) indicates success: the input procedure ends and T is returned. Otherwise, the user will be prompted to try again.

Provided Methods§

source

fn print<C: Conciliator + ?Sized>(&mut self, con: &C)

Establish the context of the request (optional, the provided method does nothing)

For example: to request a selection to be made from a List of items, this function is implemented and Prints the List. Then prompt only specifies the range of valid indices.

This differs from the signature of Print in that it takes &mut self instead self by value, because the Input implementor will still be needed to prompt and validate. Because of this, implementors of this that are wrappers around Print implementors may chose to use the if let Some(p) = self.p.take() { p.print(con) } pattern.

Object Safety§

This trait is not object safe.

Implementations on Foreign Types§

source§

impl<'s> Input for &'s str

Basic String input, no validation

§

type T = String

source§

fn prompt(&self, buffer: &mut Buffer)

source§

fn validate(&self, user_input: &str) -> Option<String>

Implementors§

source§

impl Input for AbortRetryContinue

source§

impl<Q, M> Input for Confirm<Q, M>
where for<'a> &'a Q: Pushable<M>,

§

type T = bool

source§

impl<Q, M, H, I, F, P> Input for Select<Q, M, List<H, I, F, P>>
where List<H, I, F, P>: Print, Q: Pushable<M>,

§

type T = usize