解析器按预期工作,直到我想解析h:
始终是字符串中最后一个数字的数字并且编译器给了我
^ expected &str, found struct `nom::types::CompleteStr`
我认为这是因为解析器正在向前看。我该如何阻止它,或者我如何表示它已经完成?
#[macro_use]
extern crate nom;
use nom::digit;
use nom::types::CompleteStr;
use std::str::FromStr;
#[derive(Debug, PartialEq)]
pub struct Order {
pub l: u64,
pub w: u64,
pub h: u64,
}
named!(order_parser<CompleteStr, Order>,
do_parse!(
l: map_res!(digit, u64::from_str) >>
tag!("x") >>
w: map_res!(digit, u64::from_str) >>
tag!("x") >>
h: map_res!(digit, u64::from_str) >>
(Order {l: l, w: w, h: h })
)
);
pub fn wrap_order(order: &str) -> Result<(CompleteStr, Order), nom::Err<&str>> {
order_parser(order)
}
#[test]
fn test_order_parser() {
assert_eq!(
wrap_order(CompleteStr("2x3x4")),
Ok((CompleteStr(""), Order { l: 2, w: 3, h: 4 }))
);
}