Why does converting a nom’s CompleteStr to a String in the macro named! return a Result?

220 Views Asked by At

When I try to convert nom’s CompleteStr to a String in named!, I get an error saying it’s returning a Result.

named!(letter_cs<CompleteStr,String>,
    map_res!(
        alpha,
        |CompleteStr(s)| String::from(s)
       )
); 

will throw the error

error[E0308]: mismatched types                                                                                                                          
  --> src/year2015/day_7.rs:16:1                                                                                                                        
   |                                                                                                                                                    
16 | / named!(letter_cs<CompleteStr,String>,                                                                                                            
17 | |     map_res!(                                                                                                                                    
18 | |         alpha,                                                                                                                                   
19 | |         |CompleteStr(s)| String::from(s)                                                                                                         
20 | |          )                                                                                                                                       
21 | | );                                                                                                                                               
   | |__^ expected struct `std::string::String`, found enum `std::result::Result`                                                                       
   |                                                                                                                                                    
   = note: expected type `std::string::String`                                                                                                          
              found type `std::result::Result<_, _>`                                                                                                    
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info) 
1

There are 1 best solutions below

0
On BEST ANSWER

map_res! expects a function that returns a Result as the second argument, which is why map_res! is named like this. You can also see it in its "type" in nom's doc:

map_res!(I -> IResult<I,O>, O -> Result<P>) => I -> IResult<I, P>

However, String::from does not return a result; so String::from(s) is the wrong type for map_res!. Instead, you should use the regular map!, which has this "type" map!(I -> IResult<I,O>, O -> P) => I -> IResult<I, P>:

#[macro_use]
extern crate nom;

use nom::types::CompleteStr;
use nom::alpha;

named!(letter_cs<CompleteStr,String>,
    map!(
        alpha,
        |CompleteStr(s)| String::from(s)
       )
);