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>
impl<T: Clone + Send + Sync + 'static> InputChain<T>
Sourcepub fn try_source<C: InputCollector<T> + 'static>(self, source: C) -> Self
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.
Sourcepub fn try_source_with_kind<C: InputCollector<T> + 'static>(
self,
source: C,
kind: InputSourceKind,
) -> Self
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.
Sourcepub fn validate<F>(self, f: F, error_msg: impl Into<String>) -> Self
pub fn validate<F>(self, f: F, error_msg: impl Into<String>) -> Self
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.
Sourcepub fn validate_with<F>(self, f: F) -> Self
pub fn validate_with<F>(self, f: F) -> Self
Add a validation rule that returns a Result.
Unlike validate, this allows custom error messages
per validation failure.
Sourcepub fn default(self, value: T) -> Self
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.
Sourcepub fn resolve(&self, matches: &ArgMatches) -> Result<T, InputError>
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.
Sourcepub fn resolve_with_source(
&self,
matches: &ArgMatches,
) -> Result<ResolvedInput<T>, InputError>
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.
Sourcepub fn has_available_source(&self, matches: &ArgMatches) -> bool
pub fn has_available_source(&self, matches: &ArgMatches) -> bool
Check if any source is available to provide input.
Sourcepub fn source_count(&self) -> usize
pub fn source_count(&self) -> usize
Get the number of sources in the chain.
Trait Implementations§
Source§impl<T> Debug for InputChain<T>
impl<T> Debug for InputChain<T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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