use std::env; use std::fs::File; use std::io::BufRead; use std::io::BufReader; use std::io::Error; use std::iter::Iterator; use regex::Regex; use unidecode::unidecode; #[derive(Default)] struct Dictionary { words: Vec, iter_position: usize, } /*impl Dictionary { fn default() -> Dictionary { Dictionary{words: vec![], iter_position: 0} } }*/ impl Iterator for Dictionary { type Item = String; fn next(&mut self) -> Option { if self.words.len() >= self.iter_position { None } else { let v = self.words[self.iter_position].clone(); self.iter_position += 1; Some(v) } } } fn dictionary_from_iterable(lines: impl Iterator>) -> Dictionary { let mut w = vec![]; for line in lines { let line = line.unwrap(); // TODO: normalizza: lascia solo a-z, converti gli accenti, ecc. w.push(line_to_word(line)) } return Dictionary { words: w, ..Dictionary::default() }; } fn line_to_word(l: String) -> String { let l = unidecode(&l); let l = l.to_lowercase(); l } fn main() { let args: Vec = env::args().collect(); let fname = &args[1]; let regexp = &args[2]; let f = File::open(fname).unwrap(); let buf = BufReader::new(&f); let d = dictionary_from_iterable(buf.lines()); let re = Regex::new(regexp).unwrap(); for w in d { if re.is_match(w.as_str()) { println!("{}", w) } } }