Skip to content

Commit

Permalink
fix: if the file unexpectedly ends before a newline, it no longer cra…
Browse files Browse the repository at this point in the history
…shes
  • Loading branch information
zleyyij committed Feb 9, 2024
1 parent c99dee4 commit 1fff51e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/scripts/parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
// https://github.com/rust-bakery/nom/tree/main/doc

pub mod lexer {
use nom::branch::alt;
use nom::bytes::complete::{is_not, take_till};
use nom::character::complete::char as nom_char;
use nom::combinator::eof;
use nom::{
bytes::complete::{tag, take_until},
multi::many0,
Expand Down Expand Up @@ -37,7 +40,7 @@ pub mod lexer {
///
/// * `input` - A string slice from a csv generated by HWInfo.
fn read_record(input: &str) -> IResult<&str, Vec<&str>> {
let record: (&str, &str) = terminated(take_until("\n"), tag("\n"))(input)?;
let record: (&str, &str) = terminated(alt((take_until("\n"), is_not(""))), alt((tag("\n"), take_until(""))))(input)?;
// apply the parser a bunch of times, until the input string is empty
let record_parsing_output = many0(read_field)(record.1)?;
// I want this function to integrate smoothly and behave like an idiomatic nom function
Expand Down Expand Up @@ -90,12 +93,16 @@ pub mod lexer {
assert_eq!(read_field("ab,cd,"), Ok(("cd,", "ab")));
}

// read_record
#[test]
fn basic_read_record_until_newline() {
assert_eq!(read_record("ab,cd,\n"), Ok(("", vec!["ab", "cd"])));
}

#[test]
fn read_record_until_eof() {
assert_eq!(read_record("ab,cd,"), Ok(("", vec!["ab", "cd"])));
}

#[test]
fn read_record_includes_leftovers() {
assert_eq!(
Expand Down
3 changes: 2 additions & 1 deletion src/scripts/parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ mod tests {

#[test]
fn parse_csv_from_file() {
let mut file_handle = File::open("C:\\Users\\James Boehme\\Downloads\\stress.CSV").unwrap();
// TODO
let mut file_handle = File::open("/Users/arc/Downloads/log11.csv").unwrap();
let mut file_vec = Vec::new();
file_handle.read_to_end(&mut file_vec).unwrap();
parse_csv(&file_vec);
Expand Down

0 comments on commit 1fff51e

Please sign in to comment.