nom::combinator

Function success

Source
pub fn success<I, O: Clone, E: ParseError<I>>(
    val: O,
) -> impl Fn(I) -> IResult<I, O, E>
Expand description

a parser which always succeeds with given value without consuming any input.

It can be used for example as the last alternative in alt to specify the default case.

use nom::branch::alt;
use nom::combinator::{success, value};
use nom::character::complete::char;

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

let mut sign = alt((value(-1, char('-')), value(1, char('+')), success::<_,_,(_,ErrorKind)>(1)));
assert_eq!(sign("+10"), Ok(("10", 1)));
assert_eq!(sign("-10"), Ok(("10", -1)));
assert_eq!(sign("10"), Ok(("10", 1)));