#![allow(missing_docs)]
use std::{
fs::File,
io::{BufRead, BufReader},
iter,
path::PathBuf,
};
pub struct MirasDoc {
pub text: String,
}
pub struct MirasTextReader {
path: PathBuf,
}
impl MirasTextReader {
pub fn new(path: impl Into<PathBuf>) -> Self {
Self { path: path.into() }
}
pub fn docs(&self) -> Box<dyn Iterator<Item = MirasDoc> + '_> {
let file = match File::open(&self.path) {
Ok(f) => f,
Err(e) => {
eprintln!("MirasTextReader: cannot open {:?}: {}", self.path, e);
return Box::new(iter::empty());
}
};
let docs: Vec<MirasDoc> = BufReader::new(file)
.lines()
.filter_map(|l| l.ok())
.filter_map(|line| {
let text = line.splitn(2, "***").next()?.to_string();
if text.is_empty() { None } else { Some(MirasDoc { text }) }
})
.collect();
Box::new(docs.into_iter())
}
pub fn texts(&self) -> Box<dyn Iterator<Item = String> + '_> {
Box::new(self.docs().map(|d| d.text))
}
}