rewrite woodpecker-ci to publish to crates.io #8
1 changed files with 24 additions and 35 deletions
29
src/lib.rs
29
src/lib.rs
|
@ -39,7 +39,7 @@ pub fn skip(cli: &Cli, writer: &mut impl Write) -> Result<()> {
|
||||||
None => Box::new(BufReader::new(std::io::stdin())),
|
None => Box::new(BufReader::new(std::io::stdin())),
|
||||||
};
|
};
|
||||||
if let Some(line) = &cli.line {
|
if let Some(line) = &cli.line {
|
||||||
skip_lines_matching(&cli, reader, writer, line)
|
skip_lines_matching(cli, reader, writer, line)
|
||||||
} else if let Some(ref token) = cli.token {
|
} else if let Some(ref token) = cli.token {
|
||||||
skip_tokens(cli, reader, writer, token)
|
skip_tokens(cli, reader, writer, token)
|
||||||
} else {
|
} else {
|
||||||
|
@ -49,14 +49,10 @@ pub fn skip(cli: &Cli, writer: &mut impl Write) -> Result<()> {
|
||||||
|
|
||||||
// skip a number of lines
|
// skip a number of lines
|
||||||
fn skip_lines(cli: &Cli, reader: Box<dyn BufRead>, writer: &mut impl Write) -> Result<()> {
|
fn skip_lines(cli: &Cli, reader: Box<dyn BufRead>, writer: &mut impl Write) -> Result<()> {
|
||||||
let mut counter = 0usize;
|
for (counter, current_line) in reader.lines().map_while(Option::Some).flatten().enumerate() {
|
||||||
for current_line in reader.lines() {
|
|
||||||
if let Ok(current_line) = current_line {
|
|
||||||
if counter >= cli.lines {
|
if counter >= cli.lines {
|
||||||
writeln!(writer, "{}", current_line)?;
|
writeln!(writer, "{}", current_line)?;
|
||||||
}
|
}
|
||||||
counter += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -69,8 +65,7 @@ fn skip_lines_matching(
|
||||||
line: &str,
|
line: &str,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut counter = 0usize;
|
let mut counter = 0usize;
|
||||||
for current_line in reader.lines() {
|
for current_line in reader.lines().map_while(Option::Some).flatten() {
|
||||||
if let Ok(current_line) = current_line {
|
|
||||||
if counter >= cli.lines {
|
if counter >= cli.lines {
|
||||||
writeln!(writer, "{}", current_line)?;
|
writeln!(writer, "{}", current_line)?;
|
||||||
}
|
}
|
||||||
|
@ -78,7 +73,6 @@ fn skip_lines_matching(
|
||||||
counter += 1;
|
counter += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -92,12 +86,11 @@ fn skip_tokens(
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut counter = 0usize;
|
let mut counter = 0usize;
|
||||||
|
|
||||||
for current_line in reader.lines() {
|
for current_line in reader.lines().map_while(Option::Some).flatten() {
|
||||||
if let Ok(current_line) = current_line {
|
|
||||||
if counter >= cli.lines {
|
if counter >= cli.lines {
|
||||||
writeln!(writer, "{}", current_line)?;
|
writeln!(writer, "{}", current_line)?;
|
||||||
}
|
}
|
||||||
if current_line.contains(&token) {
|
if current_line.contains(token) {
|
||||||
if cli.ignore_extras {
|
if cli.ignore_extras {
|
||||||
counter += 1;
|
counter += 1;
|
||||||
} else {
|
} else {
|
||||||
|
@ -106,7 +99,6 @@ fn skip_tokens(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -151,10 +143,7 @@ mod tests {
|
||||||
skip(&cli, &mut lines)?;
|
skip(&cli, &mut lines)?;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assert_eq!(
|
assert_eq!(String::from_utf8(lines)?, ["alpha", "gamma\n"].join("\n"));
|
||||||
String::from_utf8(lines)?,
|
|
||||||
vec!["alpha", "gamma\n"].join("\n")
|
|
||||||
);
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -196,7 +185,7 @@ mod tests {
|
||||||
//then
|
//then
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
String::from_utf8(lines)?,
|
String::from_utf8(lines)?,
|
||||||
vec![
|
[
|
||||||
"Or help one fainting robin",
|
"Or help one fainting robin",
|
||||||
"Unto his nest again,",
|
"Unto his nest again,",
|
||||||
"I shall not live in vain.\n"
|
"I shall not live in vain.\n"
|
||||||
|
@ -224,7 +213,7 @@ mod tests {
|
||||||
//then
|
//then
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
String::from_utf8(lines)?,
|
String::from_utf8(lines)?,
|
||||||
vec![
|
[
|
||||||
//Lorem ipsum dolor sit amet, -- +2 = 2
|
//Lorem ipsum dolor sit amet, -- +2 = 2
|
||||||
//consectetur adipiscing elit,
|
//consectetur adipiscing elit,
|
||||||
//sed do eiusmod tempor incididunt -- +1 = 3
|
//sed do eiusmod tempor incididunt -- +1 = 3
|
||||||
|
@ -257,7 +246,7 @@ mod tests {
|
||||||
//then
|
//then
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
String::from_utf8(lines)?,
|
String::from_utf8(lines)?,
|
||||||
vec![
|
[
|
||||||
//Lorem ipsum dolor sit amet, -- 1
|
//Lorem ipsum dolor sit amet, -- 1
|
||||||
//consectetur adipiscing elit,
|
//consectetur adipiscing elit,
|
||||||
//sed do eiusmod tempor incididunt -- 2
|
//sed do eiusmod tempor incididunt -- 2
|
||||||
|
|
Loading…
Reference in a new issue