Skip to main content

InputCollector

Trait InputCollector 

Source
pub trait InputCollector<T>: Send + Sync {
    // Required methods
    fn name(&self) -> &'static str;
    fn is_available(&self, matches: &ArgMatches) -> bool;
    fn collect(&self, matches: &ArgMatches) -> Result<Option<T>, InputError>;

    // Provided methods
    fn validate(&self, _value: &T) -> Result<(), String> { ... }
    fn can_retry(&self) -> bool { ... }
}
Expand description

A source that can collect input of type T.

Input collectors are the building blocks of input chains. Each collector represents one way to obtain input: from CLI arguments, stdin, environment variables, editors, or interactive prompts.

§Implementation Guidelines

  • is_available should return false if this source cannot provide input in the current environment (e.g., no TTY for prompts, stdin not piped for stdin source).

  • collect should return Ok(None) to indicate “try the next source” and Ok(Some(value)) when input was successfully collected. Return Err only for actual failures.

  • Interactive collectors should implement can_retry to return true, allowing validation failures to re-prompt the user.

§Example

use standout_input::{InputCollector, InputError};
use clap::ArgMatches;

struct FixedValue(String);

impl InputCollector<String> for FixedValue {
    fn name(&self) -> &'static str { "fixed" }

    fn is_available(&self, _: &ArgMatches) -> bool { true }

    fn collect(&self, _: &ArgMatches) -> Result<Option<String>, InputError> {
        Ok(Some(self.0.clone()))
    }
}

Required Methods§

Source

fn name(&self) -> &'static str

Human-readable name for this collector.

Used in error messages and debugging. Examples: “argument”, “stdin”, “editor”, “prompt”.

Source

fn is_available(&self, matches: &ArgMatches) -> bool

Check if this collector can provide input in the current environment.

Returns false if:

  • Interactive collector but no TTY available
  • Stdin source but stdin is not piped
  • Argument source but argument was not provided

The chain will skip unavailable collectors and try the next one.

Source

fn collect(&self, matches: &ArgMatches) -> Result<Option<T>, InputError>

Attempt to collect input from this source.

§Returns
  • Ok(Some(value)) - Input was successfully collected
  • Ok(None) - This source has no input; try the next one in the chain
  • Err(e) - Collection failed; abort the chain with this error

Provided Methods§

Source

fn validate(&self, _value: &T) -> Result<(), String>

Validate the collected value.

Called after successful collection. Override to add source-specific validation that can trigger re-prompting for interactive sources.

Default implementation accepts all values.

Source

fn can_retry(&self) -> bool

Whether this collector supports retry on validation failure.

Interactive collectors (prompts, editor) should return true to allow re-prompting when validation fails. Non-interactive sources (args, stdin) should return false.

Default is false.

Implementors§