28 lines
663 B
Rust
28 lines
663 B
Rust
|
|
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}");
|
||
|
|
}
|
||
|
|
}
|