Function neure::neu::may

source ·
pub fn may<T, I, U>(if: I, unit: U) -> MayUnit<U, I, T>where
    U: Neu<T>,
    I: Neu<T>,
Expand description

Match if and remember the result, then match unit

Example

    let digit = neu::digit(10);
    let hex = neu::digit(16);

    let tests = [
        ("99EF", Some(Span::new(0, 2))),
        ("0x99EF", Some(Span::new(0, 6))),
        ("099EF", Some(Span::new(0, 3))),
        ("9899", Some(Span::new(0, 4))),
        ("x99EF", None),
    ];

    for test in tests {
        // `may` is not reuseable
        let num = neu::may('0', neu::may('x', hex).or(neu::none())).or(digit);
        let num = re!((num){1,6});
        let mut ctx = CharsCtx::new(test.0);

        if let Some(span) = test.1 {
            assert_eq!(ctx.try_mat(&num)?, span, "at {}", test.0);
        } else {
            assert!(ctx.try_mat(&num).is_err());
        }
    }

    Ok(())