| 1 |
use nom::{ |
| 2 |
character::complete::{alphanumeric1 as alphanumeric, line_ending as eol}, |
| 3 |
multi::many0, |
| 4 |
sequence::terminated, |
| 5 |
IResult, |
| 6 |
}; |
| 7 |
|
| 8 |
pub fn end_of_line(input: &str) -> IResult<&str, &str> { |
| 9 |
if input.is_empty() { |
| 10 |
Ok((input, input)) |
| 11 |
} else { |
| 12 |
eol(input) |
| 13 |
} |
| 14 |
} |
| 15 |
|
| 16 |
pub fn read_line(input: &str) -> IResult<&str, &str> { |
| 17 |
terminated(alphanumeric, end_of_line)(input) |
| 18 |
} |
| 19 |
|
| 20 |
pub fn read_lines(input: &str) -> IResult<&str, Vec<&str>> { |
| 21 |
many0(read_line)(input) |
| 22 |
} |
| 23 |
|
| 24 |
#[cfg(feature = "alloc")] |
| 25 |
#[test] |
| 26 |
fn read_lines_test() { |
| 27 |
let res = Ok(("", vec!["Duck", "Dog", "Cow"])); |
| 28 |
|
| 29 |
assert_eq!(read_lines("Duck\nDog\nCow\n"), res); |
| 30 |
assert_eq!(read_lines("Duck\nDog\nCow"), res); |
| 31 |
} |