cargo fmt

This commit is contained in:
Paul Campbell 2023-03-22 06:52:40 +00:00
parent c1fdfbdcc8
commit fca15ad95a
2 changed files with 52 additions and 27 deletions

View file

@ -1,8 +1,8 @@
use std::path::PathBuf;
use std::fmt::Debug;
use clap::Parser; use clap::Parser;
use std::fmt::Debug;
use std::fs; use std::fs;
use std::io; use std::io;
use std::path::PathBuf;
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version)] #[command(version)]
@ -15,7 +15,12 @@ pub struct Cli {
#[arg(short, long, conflicts_with = "token")] #[arg(short, long, conflicts_with = "token")]
line: Option<String>, line: Option<String>,
/// Skip lines until N tokens found /// 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<String>, token: Option<String>,
/// Only count the first token on each line /// Only count the first token on each line
#[arg(short, long = "ignore-extras")] #[arg(short, long = "ignore-extras")]
@ -23,7 +28,8 @@ pub struct Cli {
} }
pub fn skip<F>(cli: &Cli, mut out: F) -> io::Result<()> pub fn skip<F>(cli: &Cli, mut out: F) -> io::Result<()>
where F: FnMut(String) -> () where
F: FnMut(String) -> (),
{ {
match &cli.file { match &cli.file {
Some(ref file) => { Some(ref file) => {
@ -34,13 +40,14 @@ pub fn skip<F>(cli: &Cli, mut out: F) -> io::Result<()>
} else { } else {
skip_file_lines(cli, out, file) skip_file_lines(cli, out, file)
} }
}, }
None => todo!("reading from stdin") None => todo!("reading from stdin"),
} }
} }
fn skip_file_lines<F>(cli: &Cli, mut out: F, file: &PathBuf) -> io::Result<()> fn skip_file_lines<F>(cli: &Cli, mut out: F, file: &PathBuf) -> io::Result<()>
where F: FnMut(String) -> () where
F: FnMut(String) -> (),
{ {
eprintln!("skip_file_lines"); eprintln!("skip_file_lines");
let content = fs::read_to_string(file).expect("Could not read file"); let content = fs::read_to_string(file).expect("Could not read file");
@ -55,7 +62,8 @@ fn skip_file_lines<F>(cli: &Cli, mut out: F, file: &PathBuf) -> io::Result<()>
} }
fn skip_file_lines_matching<F>(cli: &Cli, mut out: F, file: &PathBuf, line: &str) -> io::Result<()> fn skip_file_lines_matching<F>(cli: &Cli, mut out: F, file: &PathBuf, line: &str) -> io::Result<()>
where F: FnMut(String) -> () where
F: FnMut(String) -> (),
{ {
eprintln!("skip_file_lines_matching"); eprintln!("skip_file_lines_matching");
let content = fs::read_to_string(file).expect("Could not read file"); let content = fs::read_to_string(file).expect("Could not read file");
@ -72,7 +80,8 @@ fn skip_file_lines_matching<F>(cli: &Cli, mut out: F, file: &PathBuf, line: &str
} }
fn skip_file_tokens<F>(cli: &Cli, mut out: F, file: &PathBuf, token: &str) -> io::Result<()> fn skip_file_tokens<F>(cli: &Cli, mut out: F, file: &PathBuf, token: &str) -> io::Result<()>
where F: FnMut(String) -> () where
F: FnMut(String) -> (),
{ {
eprintln!("skip_file_lines_tokens"); eprintln!("skip_file_lines_tokens");
let content = fs::read_to_string(file).expect("Could not read file"); let content = fs::read_to_string(file).expect("Could not read file");
@ -94,9 +103,15 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn skip_one_line_from_two() -> io::Result<()> { fn skip_one_line() -> io::Result<()> {
//given //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<String> = Vec::new(); let mut lines: Vec<String> = Vec::new();
//when //when
@ -108,9 +123,15 @@ mod tests {
} }
#[test] #[test]
fn skip_two_lines_from_four() -> io::Result<()> { fn skip_two_lines() -> io::Result<()> {
//given //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<String> = Vec::new(); let mut lines: Vec<String> = Vec::new();
//when //when
@ -122,9 +143,15 @@ mod tests {
} }
#[test] #[test]
fn skip_two_matching_lines_of_alpha() -> io::Result<()> { fn skip_two_matching_lines() -> io::Result<()> {
//given //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<String> = Vec::new(); let mut lines: Vec<String> = Vec::new();
//when //when

View file

@ -1,5 +1,5 @@
use skip::{Cli, skip};
use clap::Parser; use clap::Parser;
use skip::{skip, Cli};
use std::io::Write; use std::io::Write;
fn main() -> std::io::Result<()> { fn main() -> std::io::Result<()> {
@ -8,10 +8,8 @@ fn main() -> std::io::Result<()> {
let stdout = std::io::stdout(); let stdout = std::io::stdout();
let mut output = stdout.lock(); let mut output = stdout.lock();
skip(&cli, |line| { skip(&cli, |line| match writeln!(output, "{}", line) {
match writeln!(output, "{}", line) {
Err(_) => (), Err(_) => (),
Ok(_) => () Ok(_) => (),
}
}) })
} }