From 9c51c9c719c740fde00f2ce2c3b1698ffd12fb87 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Mon, 26 Feb 2024 08:47:01 +0000 Subject: [PATCH] cargo clippy --- src/lib.rs | 59 ++++++++++++++++++++++-------------------------------- 1 file changed, 24 insertions(+), 35 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 397c4b0..a61003a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ pub fn skip(cli: &Cli, writer: &mut impl Write) -> Result<()> { None => Box::new(BufReader::new(std::io::stdin())), }; if let Some(line) = &cli.line { - skip_lines_matching(&cli, reader, writer, line) + skip_lines_matching(cli, reader, writer, line) } else if let Some(ref token) = cli.token { skip_tokens(cli, reader, writer, token) } else { @@ -49,13 +49,9 @@ pub fn skip(cli: &Cli, writer: &mut impl Write) -> Result<()> { // skip a number of lines fn skip_lines(cli: &Cli, reader: Box, writer: &mut impl Write) -> Result<()> { - let mut counter = 0usize; - for current_line in reader.lines() { - if let Ok(current_line) = current_line { - if counter >= cli.lines { - writeln!(writer, "{}", current_line)?; - } - counter += 1; + for (counter, current_line) in reader.lines().map_while(Option::Some).flatten().enumerate() { + if counter >= cli.lines { + writeln!(writer, "{}", current_line)?; } } Ok(()) @@ -69,14 +65,12 @@ fn skip_lines_matching( line: &str, ) -> Result<()> { let mut counter = 0usize; - for current_line in reader.lines() { - if let Ok(current_line) = current_line { - if counter >= cli.lines { - writeln!(writer, "{}", current_line)?; - } - if line == current_line { - counter += 1; - } + for current_line in reader.lines().map_while(Option::Some).flatten() { + if counter >= cli.lines { + writeln!(writer, "{}", current_line)?; + } + if line == current_line { + counter += 1; } } Ok(()) @@ -92,18 +86,16 @@ fn skip_tokens( ) -> Result<()> { let mut counter = 0usize; - for current_line in reader.lines() { - if let Ok(current_line) = current_line { - if counter >= cli.lines { - writeln!(writer, "{}", current_line)?; - } - if current_line.contains(&token) { - if cli.ignore_extras { - counter += 1; - } else { - let occurances = current_line.matches(&token).count(); - counter += occurances; - } + for current_line in reader.lines().map_while(Option::Some).flatten() { + if counter >= cli.lines { + writeln!(writer, "{}", current_line)?; + } + if current_line.contains(token) { + if cli.ignore_extras { + counter += 1; + } else { + let occurances = current_line.matches(&token).count(); + counter += occurances; } } } @@ -151,10 +143,7 @@ mod tests { skip(&cli, &mut lines)?; //then - assert_eq!( - String::from_utf8(lines)?, - vec!["alpha", "gamma\n"].join("\n") - ); + assert_eq!(String::from_utf8(lines)?, ["alpha", "gamma\n"].join("\n")); Ok(()) } @@ -196,7 +185,7 @@ mod tests { //then assert_eq!( String::from_utf8(lines)?, - vec![ + [ "Or help one fainting robin", "Unto his nest again,", "I shall not live in vain.\n" @@ -224,7 +213,7 @@ mod tests { //then assert_eq!( String::from_utf8(lines)?, - vec![ + [ //Lorem ipsum dolor sit amet, -- +2 = 2 //consectetur adipiscing elit, //sed do eiusmod tempor incididunt -- +1 = 3 @@ -257,7 +246,7 @@ mod tests { //then assert_eq!( String::from_utf8(lines)?, - vec![ + [ //Lorem ipsum dolor sit amet, -- 1 //consectetur adipiscing elit, //sed do eiusmod tempor incididunt -- 2