[−][src]Function combine::parser::combinator::from_str
pub fn from_str<O, P>(parser: P) -> FromStr<O, P> where
<P::Input as StreamOnce>::Error: ParseError<<P::Input as StreamOnce>::Item, <P::Input as StreamOnce>::Range, <P::Input as StreamOnce>::Position>,
P: Parser,
P::Output: StrLike,
O: FromStr,
O::Err: Display,
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"[..])));