use nom::bytes::complete::take_while;
use nom::character::complete::alpha1;
use nom::Err::Error;
use nom::IResult;
use nom::sequence::{delimited, tuple};
use nom::character::complete::multispace0;
use nom::error::{ErrorKind, ParseError};
pub fn parse_label_with_underscores<'a,E: ParseError<&'a str>>(input : &'a str) -> IResult<&'a str, String,E> {
let mut parser = delimited(
multispace0,
tuple(
(
alpha1,
take_while(|c: char| c == '_' || c.is_alphanumeric())
),
),
multispace0
);
parser(input).map(|(rem, (x, y))| {
(rem, format!("{}{}", x, y))
})
}
pub fn parse_element_of_preexisting_vec_and_return_index<'a, E: ParseError<&'a str>>(reference_vec : &[String], input : &'a str) -> IResult<&'a str, usize,E> {
match parse_label_with_underscores(input) {
Err(e) => {
Err(e)
},
Ok((rem,lab)) => {
match reference_vec.iter().position(|s| s.eq(&lab)) {
None => {
Err(Error(nom::error::make_error(input, ErrorKind::Fail)))
}
Some(index) => {
Ok((rem,index))
}
}
}
}
}