skip until matching tokens seen
This commit is contained in:
parent
fca15ad95a
commit
9b6470bf06
3 changed files with 112 additions and 5 deletions
102
src/lib.rs
102
src/lib.rs
|
@ -27,7 +27,7 @@ pub struct Cli {
|
||||||
ignore_extras: bool,
|
ignore_extras: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn skip<F>(cli: &Cli, mut out: F) -> io::Result<()>
|
pub fn skip<F>(cli: &Cli, out: F) -> io::Result<()>
|
||||||
where
|
where
|
||||||
F: FnMut(String) -> (),
|
F: FnMut(String) -> (),
|
||||||
{
|
{
|
||||||
|
@ -49,7 +49,6 @@ fn skip_file_lines<F>(cli: &Cli, mut out: F, file: &PathBuf) -> io::Result<()>
|
||||||
where
|
where
|
||||||
F: FnMut(String) -> (),
|
F: FnMut(String) -> (),
|
||||||
{
|
{
|
||||||
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");
|
||||||
let mut counter = 0usize;
|
let mut counter = 0usize;
|
||||||
for current_line in content.lines() {
|
for current_line in content.lines() {
|
||||||
|
@ -65,7 +64,6 @@ fn skip_file_lines_matching<F>(cli: &Cli, mut out: F, file: &PathBuf, line: &str
|
||||||
where
|
where
|
||||||
F: FnMut(String) -> (),
|
F: FnMut(String) -> (),
|
||||||
{
|
{
|
||||||
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");
|
||||||
let mut counter = 0usize;
|
let mut counter = 0usize;
|
||||||
for current_line in content.lines() {
|
for current_line in content.lines() {
|
||||||
|
@ -83,7 +81,6 @@ fn skip_file_tokens<F>(cli: &Cli, mut out: F, file: &PathBuf, token: &str) -> io
|
||||||
where
|
where
|
||||||
F: FnMut(String) -> (),
|
F: FnMut(String) -> (),
|
||||||
{
|
{
|
||||||
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");
|
||||||
let mut counter = 0usize;
|
let mut counter = 0usize;
|
||||||
for current_line in content.lines() {
|
for current_line in content.lines() {
|
||||||
|
@ -91,7 +88,12 @@ where
|
||||||
out(String::from(current_line));
|
out(String::from(current_line));
|
||||||
}
|
}
|
||||||
if current_line.contains(&token) {
|
if current_line.contains(&token) {
|
||||||
counter += 1;
|
if cli.ignore_extras {
|
||||||
|
counter += 1;
|
||||||
|
} else {
|
||||||
|
let occurances = current_line.matches(&token).count();
|
||||||
|
counter += occurances;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -162,4 +164,94 @@ mod tests {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn skip_three_matching_tokens() -> io::Result<()> {
|
||||||
|
//given
|
||||||
|
let cli = Cli {
|
||||||
|
lines: 3,
|
||||||
|
file: Some(PathBuf::from("tests/poem.txt")),
|
||||||
|
line: None,
|
||||||
|
token: Some(String::from("one")),
|
||||||
|
ignore_extras: false,
|
||||||
|
};
|
||||||
|
let mut lines: Vec<String> = Vec::new();
|
||||||
|
|
||||||
|
//when
|
||||||
|
skip(&cli, |line| lines.push(line))?;
|
||||||
|
|
||||||
|
//then
|
||||||
|
assert_eq!(
|
||||||
|
lines,
|
||||||
|
vec![
|
||||||
|
"Or help one fainting robin",
|
||||||
|
"Unto his nest again,",
|
||||||
|
"I shall not live in vain."
|
||||||
|
]
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn skip_three_matching_tokens_include_extras() -> io::Result<()> {
|
||||||
|
//given
|
||||||
|
let cli = Cli {
|
||||||
|
lines: 4,
|
||||||
|
file: Some(PathBuf::from("tests/lorem.txt")),
|
||||||
|
line: None,
|
||||||
|
token: Some(String::from("or")),
|
||||||
|
ignore_extras: false,
|
||||||
|
};
|
||||||
|
let mut lines: Vec<String> = Vec::new();
|
||||||
|
|
||||||
|
//when
|
||||||
|
skip(&cli, |line| lines.push(line))?;
|
||||||
|
|
||||||
|
//then
|
||||||
|
assert_eq!(
|
||||||
|
lines,
|
||||||
|
vec![
|
||||||
|
//Lorem ipsum dolor sit amet, -- +2 = 2
|
||||||
|
//consectetur adipiscing elit,
|
||||||
|
//sed do eiusmod tempor incididunt -- +1 = 3
|
||||||
|
//ut labore et dolore magna aliqua. -- +2 = 5
|
||||||
|
"Ut enim ad minim veniam,",
|
||||||
|
"quis nostrud exercitation ullamco",
|
||||||
|
"laboris nisi ut aliquip ex ea",
|
||||||
|
"commodo consequat."
|
||||||
|
]
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn skip_three_matching_tokens_ignore_extras() -> io::Result<()> {
|
||||||
|
//given
|
||||||
|
let cli = Cli {
|
||||||
|
lines: 4,
|
||||||
|
file: Some(PathBuf::from("tests/lorem.txt")),
|
||||||
|
line: None,
|
||||||
|
token: Some(String::from("or")),
|
||||||
|
ignore_extras: true,
|
||||||
|
};
|
||||||
|
let mut lines: Vec<String> = Vec::new();
|
||||||
|
|
||||||
|
//when
|
||||||
|
skip(&cli, |line| lines.push(line))?;
|
||||||
|
|
||||||
|
//then
|
||||||
|
assert_eq!(
|
||||||
|
lines,
|
||||||
|
vec![
|
||||||
|
//Lorem ipsum dolor sit amet, -- 1
|
||||||
|
//consectetur adipiscing elit,
|
||||||
|
//sed do eiusmod tempor incididunt -- 2
|
||||||
|
//ut labore et dolore magna aliqua. -- 3
|
||||||
|
//Ut enim ad minim veniam,
|
||||||
|
//quis nostrud exercitation ullamco
|
||||||
|
//laboris nisi ut aliquip ex ea -- 4
|
||||||
|
"commodo consequat."
|
||||||
|
]
|
||||||
|
);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
8
tests/lorem.txt
Normal file
8
tests/lorem.txt
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
Lorem ipsum dolor sit amet,
|
||||||
|
consectetur adipiscing elit,
|
||||||
|
sed do eiusmod tempor incididunt
|
||||||
|
ut labore et dolore magna aliqua.
|
||||||
|
Ut enim ad minim veniam,
|
||||||
|
quis nostrud exercitation ullamco
|
||||||
|
laboris nisi ut aliquip ex ea
|
||||||
|
commodo consequat.
|
7
tests/poem.txt
Normal file
7
tests/poem.txt
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
If I can stop one heart from breaking,
|
||||||
|
I shall not live in vain;
|
||||||
|
If I can ease one life the aching,
|
||||||
|
Or cool one pain,
|
||||||
|
Or help one fainting robin
|
||||||
|
Unto his nest again,
|
||||||
|
I shall not live in vain.
|
Loading…
Reference in a new issue