Function chomp::parse_only [] [src]

pub fn parse_only<'a, I, T, E, F>(
    parser: F,
    input: &'a [I]
) -> Result<T, (&'a [I], E)> where
    I: Copy + PartialEq,
    F: FnOnce(&'a [I]) -> ParseResult<&'a [I], T, E>, 

Runs the given parser on the supplied finite input.

use chomp::prelude::{parse_only, Error};
use chomp::ascii::decimal;

assert_eq!(parse_only(decimal, b"123foobar"), Ok(123u32));

// Annotation because `decimal` is generic over number types
let r: Result<u32, _> = parse_only(decimal, b"foobar");
assert_eq!(r, Err((&b"foobar"[..], Error::new())));

This will not force the parser to consume all available input, any remainder will be discarded. To force a parser to consume all its input, use eof at the end like this:

use chomp::prelude::{U8Input, Error, SimpleResult, parse_only, string, eof};

fn my_parser<I: U8Input>(i: I) -> SimpleResult<I, I::Buffer> {
    parse!{i;
        let r = string(b"pattern");
                eof();

        ret r
    }
}

assert_eq!(parse_only(my_parser, b"pattern"), Ok(&b"pattern"[..]));
assert_eq!(parse_only(my_parser, b"pattern and more"),
           Err((&b" and more"[..], Error::new())));