Function chomp::parse_only [] [src]

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

Runs the given parser on the supplied finite input.

use chomp::{ParseError, Error};
use chomp::parse_only;
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(ParseError::Error(&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::{Input, ParseError, Error, U8Result};
use chomp::{parse_only, string, eof};

fn my_parser(i: Input<u8>) -> U8Result<&[u8]> {
    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(ParseError::Error(&b" and more"[..], Error::new())));