Function winnow::combinator::success
source · pub fn success<I: Stream, O: Clone, E: ParserError<I>>(
val: O,
) -> impl Parser<I, O, E>
Expand description
Always succeeds with given value without consuming any input.
For example, it can be used as the last alternative in alt
to
specify the default case.
Note: This never advances the Stream
§Example
use winnow::combinator::alt;
use winnow::combinator::success;
let mut parser = success::<_,_,InputError<_>>(10);
assert_eq!(parser.parse_peek("xyz"), Ok(("xyz", 10)));
fn sign(input: &str) -> IResult<&str, isize> {
alt((
'-'.value(-1),
'+'.value(1),
success::<_,_,InputError<_>>(1)
)).parse_peek(input)
}
assert_eq!(sign("+10"), Ok(("10", 1)));
assert_eq!(sign("-10"), Ok(("10", -1)));
assert_eq!(sign("10"), Ok(("10", 1)));