Browse Source

slight refactoring of load_from_iterable

boyska 4 years ago
parent
commit
244406a71f
1 changed files with 14 additions and 17 deletions
  1. 14 17
      src/main.rs

+ 14 - 17
src/main.rs

@@ -9,18 +9,24 @@ use unidecode::unidecode;
 use regex::Regex;
 
 #[derive(Default)]
-struct Dictionary {
+struct  Dictionary{
     words: Vec<String>,
     iter_position: usize,
 }
 
-/*impl Dictionary {
-    fn default() -> Dictionary {
-        Dictionary{words: vec![], iter_position: 0}
+impl Dictionary {
+    fn load_from_iterable(&mut self, lines: impl Iterator<Item = Result<String, Error>>) {
+        let mut it = lines;
+        for line in it.by_ref() {
+            let line = line.unwrap();
+            let word = line_to_word(line);
+            self.words.push(word);
+        }
     }
-}*/
 
-impl Iterator for Dictionary {
+}
+
+impl Iterator for Dictionary{
     type Item = String;
     fn next(&mut self) -> Option<Self::Item> {
         if self.words.len() >= self.iter_position {
@@ -33,16 +39,6 @@ impl Iterator for Dictionary {
     }
 }
 
-fn dictionary_from_iterable(lines: impl Iterator<Item = Result<String, Error>>) -> 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();
@@ -56,7 +52,8 @@ fn main() {
 
     let f = File::open(fname).unwrap();
     let buf = BufReader::new(&f);
-    let d = dictionary_from_iterable(buf.lines());
+    let mut d = Dictionary::default();
+    d.load_from_iterable(buf.lines());
 
     let re = Regex::new(regexp).unwrap();
     for w in d {