From 9b6470bf0600b14fce30a769fd373aa29e79b525 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Wed, 22 Mar 2023 06:59:25 +0000 Subject: [PATCH] skip until matching tokens seen --- src/lib.rs | 102 +++++++++++++++++++++++++++++++++++++++++++++--- tests/lorem.txt | 8 ++++ tests/poem.txt | 7 ++++ 3 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 tests/lorem.txt create mode 100644 tests/poem.txt diff --git a/src/lib.rs b/src/lib.rs index c9f12be..20ca087 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -27,7 +27,7 @@ pub struct Cli { ignore_extras: bool, } -pub fn skip(cli: &Cli, mut out: F) -> io::Result<()> +pub fn skip(cli: &Cli, out: F) -> io::Result<()> where F: FnMut(String) -> (), { @@ -49,7 +49,6 @@ fn skip_file_lines(cli: &Cli, mut out: F, file: &PathBuf) -> io::Result<()> where F: FnMut(String) -> (), { - eprintln!("skip_file_lines"); let content = fs::read_to_string(file).expect("Could not read file"); let mut counter = 0usize; for current_line in content.lines() { @@ -65,7 +64,6 @@ fn skip_file_lines_matching(cli: &Cli, mut out: F, file: &PathBuf, line: &str where F: FnMut(String) -> (), { - eprintln!("skip_file_lines_matching"); let content = fs::read_to_string(file).expect("Could not read file"); let mut counter = 0usize; for current_line in content.lines() { @@ -83,7 +81,6 @@ fn skip_file_tokens(cli: &Cli, mut out: F, file: &PathBuf, token: &str) -> io where F: FnMut(String) -> (), { - eprintln!("skip_file_lines_tokens"); let content = fs::read_to_string(file).expect("Could not read file"); let mut counter = 0usize; for current_line in content.lines() { @@ -91,7 +88,12 @@ where out(String::from(current_line)); } if current_line.contains(&token) { - counter += 1; + if cli.ignore_extras { + counter += 1; + } else { + let occurances = current_line.matches(&token).count(); + counter += occurances; + } } } Ok(()) @@ -162,4 +164,94 @@ mod tests { Ok(()) } + #[test] + fn skip_three_matching_tokens() -> io::Result<()> { + //given + let cli = Cli { + lines: 3, + file: Some(PathBuf::from("tests/poem.txt")), + line: None, + token: Some(String::from("one")), + ignore_extras: false, + }; + let mut lines: Vec = Vec::new(); + + //when + skip(&cli, |line| lines.push(line))?; + + //then + assert_eq!( + lines, + vec![ + "Or help one fainting robin", + "Unto his nest again,", + "I shall not live in vain." + ] + ); + Ok(()) + } + + #[test] + fn skip_three_matching_tokens_include_extras() -> io::Result<()> { + //given + let cli = Cli { + lines: 4, + file: Some(PathBuf::from("tests/lorem.txt")), + line: None, + token: Some(String::from("or")), + ignore_extras: false, + }; + let mut lines: Vec = Vec::new(); + + //when + skip(&cli, |line| lines.push(line))?; + + //then + assert_eq!( + lines, + vec![ + //Lorem ipsum dolor sit amet, -- +2 = 2 + //consectetur adipiscing elit, + //sed do eiusmod tempor incididunt -- +1 = 3 + //ut labore et dolore magna aliqua. -- +2 = 5 + "Ut enim ad minim veniam,", + "quis nostrud exercitation ullamco", + "laboris nisi ut aliquip ex ea", + "commodo consequat." + ] + ); + Ok(()) + } + + #[test] + fn skip_three_matching_tokens_ignore_extras() -> io::Result<()> { + //given + let cli = Cli { + lines: 4, + file: Some(PathBuf::from("tests/lorem.txt")), + line: None, + token: Some(String::from("or")), + ignore_extras: true, + }; + let mut lines: Vec = Vec::new(); + + //when + skip(&cli, |line| lines.push(line))?; + + //then + assert_eq!( + lines, + vec![ + //Lorem ipsum dolor sit amet, -- 1 + //consectetur adipiscing elit, + //sed do eiusmod tempor incididunt -- 2 + //ut labore et dolore magna aliqua. -- 3 + //Ut enim ad minim veniam, + //quis nostrud exercitation ullamco + //laboris nisi ut aliquip ex ea -- 4 + "commodo consequat." + ] + ); + Ok(()) + } } diff --git a/tests/lorem.txt b/tests/lorem.txt new file mode 100644 index 0000000..61f2dc3 --- /dev/null +++ b/tests/lorem.txt @@ -0,0 +1,8 @@ +Lorem ipsum dolor sit amet, +consectetur adipiscing elit, +sed do eiusmod tempor incididunt +ut labore et dolore magna aliqua. +Ut enim ad minim veniam, +quis nostrud exercitation ullamco +laboris nisi ut aliquip ex ea +commodo consequat. diff --git a/tests/poem.txt b/tests/poem.txt new file mode 100644 index 0000000..5203679 --- /dev/null +++ b/tests/poem.txt @@ -0,0 +1,7 @@ +If I can stop one heart from breaking, +I shall not live in vain; +If I can ease one life the aching, +Or cool one pain, +Or help one fainting robin +Unto his nest again, +I shall not live in vain.