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_availableshould returnfalseif this source cannot provide input in the current environment (e.g., no TTY for prompts, stdin not piped for stdin source). -
collectshould returnOk(None)to indicate “try the next source” andOk(Some(value))when input was successfully collected. ReturnErronly for actual failures. -
Interactive collectors should implement
can_retryto returntrue, 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§
Sourcefn name(&self) -> &'static str
fn name(&self) -> &'static str
Human-readable name for this collector.
Used in error messages and debugging. Examples: “argument”, “stdin”, “editor”, “prompt”.
Sourcefn is_available(&self, matches: &ArgMatches) -> bool
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.
Sourcefn collect(&self, matches: &ArgMatches) -> Result<Option<T>, InputError>
fn collect(&self, matches: &ArgMatches) -> Result<Option<T>, InputError>
Attempt to collect input from this source.
§Returns
Ok(Some(value))- Input was successfully collectedOk(None)- This source has no input; try the next one in the chainErr(e)- Collection failed; abort the chain with this error