File: multiline.rs

package info (click to toggle)
rust-nom-4 4.2.3-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 820 kB
  • sloc: makefile: 2
file content (30 lines) | stat: -rw-r--r-- 759 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#[macro_use]
extern crate nom;

use nom::types::CompleteStr;
use nom::{alphanumeric, eol};
use nom::IResult;

pub fn end_of_line(input: CompleteStr) -> IResult<CompleteStr, CompleteStr> {
  alt!(input, eof!() | eol)
}

pub fn read_line(input: CompleteStr) -> IResult<CompleteStr, CompleteStr> {
  terminated!(input, alphanumeric, end_of_line)
}

pub fn read_lines(input: CompleteStr) -> IResult<CompleteStr, Vec<CompleteStr>> {
  many0!(input, read_line)
}

#[cfg(feature = "alloc")]
#[test]
fn read_lines_test() {
  let res = Ok((
    CompleteStr(""),
    vec![CompleteStr("Duck"), CompleteStr("Dog"), CompleteStr("Cow")],
  ));

  assert_eq!(read_lines(CompleteStr("Duck\nDog\nCow\n")), res);
  assert_eq!(read_lines(CompleteStr("Duck\nDog\nCow")), res);
}