diff --git a/src/lib.rs b/src/lib.rs index 7e89d00..c9f12be 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,10 @@ -use std::path::PathBuf; -use std::fmt::Debug; use clap::Parser; +use std::fmt::Debug; use std::fs; use std::io; +use std::path::PathBuf; -#[derive(Parser,Debug)] +#[derive(Parser, Debug)] #[command(version)] pub struct Cli { /// The number of lines to skip @@ -12,18 +12,24 @@ pub struct Cli { /// The file to read, or stdin if not given file: Option, /// Skip until N lines matching this - #[arg(short,long, conflicts_with = "token")] + #[arg(short, long, conflicts_with = "token")] line: Option, /// Skip lines until N tokens found - #[arg(short,long, conflicts_with = "line", required_if_eq("ignore_extras", "true"))] + #[arg( + short, + long, + conflicts_with = "line", + required_if_eq("ignore_extras", "true") + )] token: Option, /// Only count the first token on each line - #[arg(short, long="ignore-extras")] + #[arg(short, long = "ignore-extras")] ignore_extras: bool, } pub fn skip(cli: &Cli, mut out: F) -> io::Result<()> - where F: FnMut(String) -> () +where + F: FnMut(String) -> (), { match &cli.file { Some(ref file) => { @@ -34,13 +40,14 @@ pub fn skip(cli: &Cli, mut out: F) -> io::Result<()> } else { skip_file_lines(cli, out, file) } - }, - None => todo!("reading from stdin") + } + None => todo!("reading from stdin"), } } -fn skip_file_lines(cli: &Cli, mut out: F, file: &PathBuf) -> io::Result<()> - where F: FnMut(String) -> () +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"); @@ -54,8 +61,9 @@ fn skip_file_lines(cli: &Cli, mut out: F, file: &PathBuf) -> io::Result<()> Ok(()) } -fn skip_file_lines_matching(cli: &Cli, mut out: F, file: &PathBuf, line: &str) -> io::Result<()> - where F: FnMut(String) -> () +fn skip_file_lines_matching(cli: &Cli, mut out: F, file: &PathBuf, line: &str) -> io::Result<()> +where + F: FnMut(String) -> (), { eprintln!("skip_file_lines_matching"); let content = fs::read_to_string(file).expect("Could not read file"); @@ -71,8 +79,9 @@ fn skip_file_lines_matching(cli: &Cli, mut out: F, file: &PathBuf, line: &str Ok(()) } -fn skip_file_tokens(cli: &Cli, mut out: F, file: &PathBuf, token: &str) -> io::Result<()> - where F: FnMut(String) -> () +fn skip_file_tokens(cli: &Cli, mut out: F, file: &PathBuf, token: &str) -> io::Result<()> +where + F: FnMut(String) -> (), { eprintln!("skip_file_lines_tokens"); let content = fs::read_to_string(file).expect("Could not read file"); @@ -94,9 +103,15 @@ mod tests { use super::*; #[test] - fn skip_one_line_from_two() -> io::Result<()> { + fn skip_one_line() -> io::Result<()> { //given - let cli = Cli { lines: 1, file: Some(PathBuf::from("tests/two-lines.txt")), line: None, token: None, ignore_extras: false }; + let cli = Cli { + lines: 1, + file: Some(PathBuf::from("tests/two-lines.txt")), + line: None, + token: None, + ignore_extras: false, + }; let mut lines: Vec = Vec::new(); //when @@ -108,9 +123,15 @@ mod tests { } #[test] - fn skip_two_lines_from_four() -> io::Result<()> { + fn skip_two_lines() -> io::Result<()> { //given - let cli = Cli { lines: 2, file: Some(PathBuf::from("tests/four-lines.txt")), line: None, token: None, ignore_extras: false }; + let cli = Cli { + lines: 2, + file: Some(PathBuf::from("tests/four-lines.txt")), + line: None, + token: None, + ignore_extras: false, + }; let mut lines: Vec = Vec::new(); //when @@ -122,9 +143,15 @@ mod tests { } #[test] - fn skip_two_matching_lines_of_alpha() -> io::Result<()> { + fn skip_two_matching_lines() -> io::Result<()> { //given - let cli = Cli { lines: 2, file: Some(PathBuf::from("tests/four-lines.txt")), line: Some(String::from("alpha")), token: None, ignore_extras: false }; + let cli = Cli { + lines: 2, + file: Some(PathBuf::from("tests/four-lines.txt")), + line: Some(String::from("alpha")), + token: None, + ignore_extras: false, + }; let mut lines: Vec = Vec::new(); //when diff --git a/src/main.rs b/src/main.rs index 418d4ca..87913ce 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ -use skip::{Cli, skip}; use clap::Parser; +use skip::{skip, Cli}; use std::io::Write; fn main() -> std::io::Result<()> { @@ -8,10 +8,8 @@ fn main() -> std::io::Result<()> { let stdout = std::io::stdout(); let mut output = stdout.lock(); - skip(&cli, |line| { - match writeln!(output, "{}", line) { - Err(_) => (), - Ok(_) => () - } + skip(&cli, |line| match writeln!(output, "{}", line) { + Err(_) => (), + Ok(_) => (), }) }