use std::{
fs::File,
io::{BufRead, BufReader},
path::Path,
};
pub fn lines<P: AsRef<Path>>(path: P) -> impl Iterator<Item = String> {
let file = File::open(&path);
match file {
Ok(f) => LineIterator {
rd: BufReader::new(f).lines(),
},
Err(err) => panic!(
"Error opening {}: {err}",
path.as_ref().as_os_str().to_string_lossy()
),
}
}
struct LineIterator {
rd: std::io::Lines<BufReader<File>>,
}
impl Iterator for LineIterator {
type Item = String;
fn next(&mut self) -> Option<Self::Item> {
match self.rd.next() {
Some(res2) => {
match res2 {
Ok(str) => Some(str), Err(_) => panic!("Cannot read file"), }
}
None => None, }
}
}