cargo fmt
This commit is contained in:
parent
c1fdfbdcc8
commit
fca15ad95a
2 changed files with 52 additions and 27 deletions
63
src/lib.rs
63
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<PathBuf>,
|
||||
/// Skip until N lines matching this
|
||||
#[arg(short,long, conflicts_with = "token")]
|
||||
#[arg(short, long, conflicts_with = "token")]
|
||||
line: Option<String>,
|
||||
/// 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>,
|
||||
/// Only count the first token on each line
|
||||
#[arg(short, long="ignore-extras")]
|
||||
#[arg(short, long = "ignore-extras")]
|
||||
ignore_extras: bool,
|
||||
}
|
||||
|
||||
pub fn skip<F>(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<F>(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<F>(cli: &Cli, mut out: F, file: &PathBuf) -> io::Result<()>
|
||||
where F: FnMut(String) -> ()
|
||||
where
|
||||
F: FnMut(String) -> (),
|
||||
{
|
||||
eprintln!("skip_file_lines");
|
||||
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<()>
|
||||
where F: FnMut(String) -> ()
|
||||
where
|
||||
F: FnMut(String) -> (),
|
||||
{
|
||||
eprintln!("skip_file_lines_matching");
|
||||
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<()>
|
||||
where F: FnMut(String) -> ()
|
||||
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<String> = 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<String> = 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<String> = Vec::new();
|
||||
|
||||
//when
|
||||
|
|
|
@ -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) {
|
||||
skip(&cli, |line| match writeln!(output, "{}", line) {
|
||||
Err(_) => (),
|
||||
Ok(_) => ()
|
||||
}
|
||||
Ok(_) => (),
|
||||
})
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue