Function winnow::combinator::success

source ·
pub fn success<I: Stream, O: Clone, E: ParseError<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::branch::alt;
use winnow::combinator::success;

let mut parser = success::<_,_,Error<_>>(10);
assert_eq!(parser.parse_next("xyz"), Ok(("xyz", 10)));

fn sign(input: &str) -> IResult<&str, isize> {
    alt((
        '-'.value(-1),
        '+'.value(1),
        success::<_,_,Error<_>>(1)
    )).parse_next(input)
}
assert_eq!(sign("+10"), Ok(("10", 1)));
assert_eq!(sign("-10"), Ok(("10", -1)));
assert_eq!(sign("10"), Ok(("10", 1)));