#[cfg(feature = "nom")] use nom::{error::Error, IResult}; #[cfg(feature = "nom")] pub trait IntoResult { type Error; fn into_result(self) -> Result; } #[cfg(feature = "nom")] impl IntoResult for IResult { type Error = nom::Err>; fn into_result(self) -> Result { self.map(|(_remaining, value)| value) } } #[cfg(all(test, feature = "nom"))] mod tests { use super::*; use nom::character::complete::char as c; fn parse_char(input: &str) -> IResult<&str, char> { c('A')(input) } #[test] fn test_into_result() { let i_result = parse_char("ABC"); assert_eq!(i_result.into_result(), Ok('A')); } #[test] fn test_into_result_error() { let i_result = parse_char("BC"); assert_eq!( i_result.into_result(), Err(nom::Err::Error(Error::new( "BC", nom::error::ErrorKind::Char ))) ); } }