feat: implement an iterator to get the test with a test consumer
This commit is contained in:
parent
3138c5de7f
commit
452483b1b3
3 changed files with 36 additions and 1 deletions
|
|
@ -1,4 +1,5 @@
|
|||
use clap::Parser;
|
||||
mod parser;
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Cli {
|
||||
|
|
@ -6,9 +7,14 @@ struct Cli {
|
|||
file_path: String,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let lines_iter = parser::read_lines(&cli.file_path)?;
|
||||
parser::consume_lines(lines_iter);
|
||||
|
||||
println!("Pattern: {}", cli.pattern);
|
||||
println!("File path: {}", cli.file_path);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
|||
27
src/parser.rs
Normal file
27
src/parser.rs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
use std::{
|
||||
fs::File,
|
||||
io::{BufRead, BufReader},
|
||||
};
|
||||
|
||||
use anyhow::Context;
|
||||
|
||||
pub fn read_lines(
|
||||
file_path: &str,
|
||||
) -> anyhow::Result<impl Iterator<Item = std::io::Result<String>>, anyhow::Error> {
|
||||
if file_path.is_empty() {
|
||||
return Err(anyhow::anyhow!("File path cannot be empty"));
|
||||
}
|
||||
|
||||
let file =
|
||||
File::open(file_path).with_context(|| format!("failed to open file: {file_path}"))?;
|
||||
let reader = BufReader::new(file);
|
||||
|
||||
Ok(reader.lines())
|
||||
}
|
||||
|
||||
pub fn consume_lines(iter: impl Iterator<Item = std::io::Result<String>>) {
|
||||
for line in iter {
|
||||
let line = line.unwrap_or("".into());
|
||||
println!("{line}");
|
||||
}
|
||||
}
|
||||
2
test.txt
Normal file
2
test.txt
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
hola
|
||||
no
|
||||
Loading…
Reference in a new issue