Function combine::from_str[][src]

pub fn from_str<Input, O, P>(parser: P) -> FromStr<Input, O, P> where
    <Input as StreamOnce>::Error: ParseError<<Input as StreamOnce>::Token, <Input as StreamOnce>::Range, <Input as StreamOnce>::Position>,
    Input: Stream,
    P: Parser<Input>,
    P::Output: StrLike,
    O: FromStr,
    O::Err: Display
Expand description

Takes a parser that outputs a string like value (&str, String, &[u8] or Vec<u8>) and parses it using std::str::FromStr. Errors if the output of parser is not UTF-8 or if FromStr::from_str returns an error.

let mut parser = from_str(many1::<String, _, _>(char::digit()));
let result = parser.parse("12345\r\n");
assert_eq!(result, Ok((12345i32, "\r\n")));

// Range parsers work as well
let mut parser = from_str(range::take_while1(|c: char| c.is_digit(10)));
let result = parser.parse("12345\r\n");
assert_eq!(result, Ok((12345i32, "\r\n")));

// As do parsers that work with bytes
let digits = || range::take_while1(|b: u8| b >= b'0' && b <= b'9');
let mut parser = from_str(range::recognize((
    digits(),
    byte::byte(b'.'),
    digits(),
)));
let result = parser.parse(&b"123.45\r\n"[..]);
assert_eq!(result, Ok((123.45f64, &b"\r\n"[..])));