Skip to main content

InputChain

Struct InputChain 

Source
pub struct InputChain<T> { /* private fields */ }
Expand description

Chain multiple input sources with fallback behavior.

Sources are tried in the order they were added. The first source that returns Some(value) wins. If all sources return None, the chain uses the default value or returns InputError::NoInput.

§Example

use standout_input::{InputChain, ArgSource, StdinSource, DefaultSource};

// Try argument first, then stdin, then use default
let chain = InputChain::<String>::new()
    .try_source(ArgSource::new("message"))
    .try_source(StdinSource::new())
    .try_source(DefaultSource::new("default message".to_string()));

let value = chain.resolve(&matches)?;

§Validation

Add validators to check the resolved value:

let chain = InputChain::<String>::new()
    .try_source(ArgSource::new("email"))
    .validate(|s| s.contains('@'), "Must be a valid email");

Interactive sources (prompts, editor) can retry on validation failure.

Implementations§

Source§

impl<T: Clone + Send + Sync + 'static> InputChain<T>

Source

pub fn new() -> Self

Create a new empty input chain.

Source

pub fn try_source<C: InputCollector<T> + 'static>(self, source: C) -> Self

Add a source to the chain.

Sources are tried in the order they are added.

Source

pub fn try_source_with_kind<C: InputCollector<T> + 'static>( self, source: C, kind: InputSourceKind, ) -> Self

Add a source with an explicit kind.

Use this when the source name doesn’t map to a standard kind.

Source

pub fn validate<F>(self, f: F, error_msg: impl Into<String>) -> Self
where F: Fn(&T) -> bool + Send + Sync + 'static,

Add a validation rule.

The validator is called after a source successfully provides input. If validation fails:

  • Interactive sources (where can_retry() is true) will re-prompt
  • Non-interactive sources will return a validation error

Multiple validators are checked in order; all must pass.

Source

pub fn validate_with<F>(self, f: F) -> Self
where F: Fn(&T) -> Result<(), String> + Send + Sync + 'static,

Add a validation rule that returns a Result.

Unlike validate, this allows custom error messages per validation failure.

Source

pub fn default(self, value: T) -> Self

Set a default value to use when no source provides input.

This is equivalent to adding a DefaultSource at the end of the chain.

Source

pub fn resolve(&self, matches: &ArgMatches) -> Result<T, InputError>

Resolve the chain and return the input value.

Tries each source in order until one provides input, then runs validation. Returns the value or an error.

Source

pub fn resolve_with_source( &self, matches: &ArgMatches, ) -> Result<ResolvedInput<T>, InputError>

Resolve the chain and return the input with source metadata.

Like resolve, but also returns which source provided the value.

Source

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

Check if any source is available to provide input.

Source

pub fn source_count(&self) -> usize

Get the number of sources in the chain.

Trait Implementations§

Source§

impl<T> Debug for InputChain<T>

Source§

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

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

impl<T: Clone + Send + Sync + 'static> Default for InputChain<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<T> Freeze for InputChain<T>
where T: Freeze,

§

impl<T> !RefUnwindSafe for InputChain<T>

§

impl<T> Send for InputChain<T>
where T: Send,

§

impl<T> Sync for InputChain<T>
where T: Sync,

§

impl<T> Unpin for InputChain<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for InputChain<T>
where T: UnsafeUnpin,

§

impl<T> !UnwindSafe for InputChain<T>

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.