refactoring

This commit is contained in:
Paul Campbell 2023-03-23 14:49:56 +00:00
parent 85896bdb86
commit 3dc4d94847

View file

@ -38,15 +38,16 @@ pub fn skip(cli: &Cli, writer: &mut impl Write) -> Result<()> {
None => Box::new(BufReader::new(std::io::stdin())), None => Box::new(BufReader::new(std::io::stdin())),
}; };
if let Some(line) = &cli.line { if let Some(line) = &cli.line {
skip_file_lines_matching(&cli, reader, writer, line) skip_lines_matching(&cli, reader, writer, line)
} else if let Some(ref token) = cli.token { } else if let Some(ref token) = cli.token {
skip_file_tokens(cli, reader, writer, token) skip_tokens(cli, reader, writer, token)
} else { } else {
skip_file_lines(cli, reader, writer) skip_lines(cli, reader, writer)
} }
} }
fn skip_file_lines(cli: &Cli, reader: Box<dyn BufRead>, writer: &mut impl Write) -> Result<()> { // skip a number of lines
fn skip_lines(cli: &Cli, reader: Box<dyn BufRead>, writer: &mut impl Write) -> Result<()> {
let mut counter = 0usize; let mut counter = 0usize;
for current_line in reader.lines() { for current_line in reader.lines() {
if let Ok(current_line) = current_line { if let Ok(current_line) = current_line {
@ -59,7 +60,8 @@ fn skip_file_lines(cli: &Cli, reader: Box<dyn BufRead>, writer: &mut impl Write)
Ok(()) Ok(())
} }
fn skip_file_lines_matching( // skip until a number of matching lines seen
fn skip_lines_matching(
cli: &Cli, cli: &Cli,
reader: Box<dyn BufRead>, reader: Box<dyn BufRead>,
writer: &mut impl Write, writer: &mut impl Write,
@ -79,7 +81,9 @@ fn skip_file_lines_matching(
Ok(()) Ok(())
} }
fn skip_file_tokens( // skip until a number of tokens seen
// may or may not count each occurance of token on a line - see cli.ignore_extras
fn skip_tokens(
cli: &Cli, cli: &Cli,
reader: Box<dyn BufRead>, reader: Box<dyn BufRead>,
writer: &mut impl Write, writer: &mut impl Write,