Why does nom expect a &str when I pass a CompleteStr?

596 Views Asked by At

The parser works as expected until I want to parse the h: digit which is always the last digit in the string and the compiler gives me

^ expected &str, found struct `nom::types::CompleteStr`

I assume it's because the parser is looking ahead. How do I stop that, or how can I signify it's done?

#[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 }))
    );
}
1

There are 1 best solutions below

0
On BEST ANSWER

The error is not on the last digit parser, it's on each of them (Rust 1.30.0 prints the error three times). That's because u64::from_str works on &str, not CompleteStr.

You can fix your parsers to use u64::from_str correctly this way:

do_parse!(
    l: map_res!(digit, |CompleteStr(s)| u64::from_str(s)) >>
    tag!("x") >>
    w: map_res!(digit, |CompleteStr(s)| u64::from_str(s)) >>
    tag!("x") >>
    h: map_res!(digit, |CompleteStr(s)| u64::from_str(s)) >>
    (Order { l: l, w: w, h: h })
)

There are also some unrelated error with the next function, which can be fixed by using the appropriate types in the signature:

pub fn wrap_order(order: &str) -> Result<(CompleteStr, Order), nom::Err<CompleteStr>> {
    order_parser(CompleteStr(order))
}