cargo fmt
This commit is contained in:
parent
c1fdfbdcc8
commit
fca15ad95a
2 changed files with 52 additions and 27 deletions
69
src/lib.rs
69
src/lib.rs
|
@ -1,10 +1,10 @@
|
||||||
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)]
|
||||||
pub struct Cli {
|
pub struct Cli {
|
||||||
/// The number of lines to skip
|
/// The number of lines to skip
|
||||||
|
@ -12,18 +12,24 @@ pub struct Cli {
|
||||||
/// The file to read, or stdin if not given
|
/// The file to read, or stdin if not given
|
||||||
file: Option<PathBuf>,
|
file: Option<PathBuf>,
|
||||||
/// Skip until N lines matching this
|
/// Skip until N lines matching this
|
||||||
#[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")]
|
||||||
ignore_extras: bool,
|
ignore_extras: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
||||||
|
@ -54,8 +61,9 @@ fn skip_file_lines<F>(cli: &Cli, mut out: F, file: &PathBuf) -> io::Result<()>
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
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");
|
||||||
|
@ -71,8 +79,9 @@ fn skip_file_lines_matching<F>(cli: &Cli, mut out: F, file: &PathBuf, line: &str
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -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(_) => ()
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue