skip/src/lib.rs

48 lines
1.1 KiB
Rust
Raw Normal View History

2023-03-20 21:33:10 +00:00
use std::path::PathBuf;
use std::fmt::Debug;
use clap::Parser;
#[derive(Parser,Debug)]
#[command(version)]
pub struct Cli {
/// The number of lines to skip
lines: usize,
/// The file to read, or stdin if not given
file: Option<PathBuf>,
/// Skip until N lines matching this
#[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"))]
token: Option<String>,
/// Only count the first token on each line
#[arg(short, long="ignore-extras")]
ignore_extras: bool,
}
pub fn skip<F>(cli: Cli, mut out: F) -> ()
where F: FnMut(String) -> ()
{
out(String::from("line 2"));
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn skip_one_line() {
//given
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
skip(cli, |line| lines.push(line));
//then
assert_eq!(lines, vec!["line 2"]);
}
}