Skip to content

Commit 34ef683

Browse files
committed
Add helpers for leftover and poisoned states
1 parent 098f84f commit 34ef683

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/lib.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,59 @@ where
511511
&self.name
512512
}
513513

514+
/// Returns `true` if the parser is in a poisoned state due to a previous error.
515+
///
516+
/// When poisoned, `forward()` will always return `Ok(None)`. Use `into_inner()`
517+
/// to recover the underlying iterator if needed.
518+
///
519+
/// # Examples
520+
///
521+
/// ```rust
522+
/// use sap::{Parser, Argument};
523+
///
524+
/// let mut parser = Parser::from_arbitrary(["prog", "--file=test"]).unwrap();
525+
///
526+
/// assert!(!parser.is_poisoned());
527+
///
528+
/// // Parse option without consuming value
529+
/// parser.forward().unwrap();
530+
///
531+
/// // This errors and poisons the parser
532+
/// assert!(parser.forward().is_err());
533+
/// assert!(parser.is_poisoned());
534+
/// ```
535+
pub const fn is_poisoned(&self) -> bool {
536+
matches!(self.state, State::Poisoned)
537+
}
538+
539+
/// Returns `true` if there is an unconsumed value from a previous option.
540+
///
541+
/// This occurs when parsing options like `--file=value` where the value has not
542+
/// yet been retrieved via `value()` or discarded via `ignore_value()`.
543+
///
544+
/// # Examples
545+
///
546+
/// ```rust
547+
/// use sap::{Parser, Argument};
548+
///
549+
/// let mut parser = Parser::from_arbitrary(["prog", "--file=test.txt"]).unwrap();
550+
///
551+
/// assert!(!parser.has_leftover_value());
552+
///
553+
/// // Parse the option
554+
/// parser.forward().unwrap();
555+
///
556+
/// // Now there's a leftover value
557+
/// assert!(parser.has_leftover_value());
558+
///
559+
/// // Consume it
560+
/// parser.value();
561+
/// assert!(!parser.has_leftover_value());
562+
/// ```
563+
pub const fn has_leftover_value(&self) -> bool {
564+
matches!(self.state, State::LeftoverValue(_))
565+
}
566+
514567
/// Consumes the parser and returns the underlying iterator.
515568
///
516569
/// This allows access to any remaining, unparsed arguments. Note that the

0 commit comments

Comments
 (0)