Browse code

fix losing part of the state on value change

Julio Biason authored on 28/02/2019 09:24:19
Showing 1 changed files

... ...
@@ -5,12 +5,13 @@ use std::io::prelude::*;
5 5
 use std::path::Path;
6 6
 use slug::slugify;
7 7
 
8
+#[derive(Debug)]
8 9
 enum CurrentTag {
9 10
     Title,
10 11
     Data
11 12
 }
12 13
 
13
-#[derive(Default)]
14
+#[derive(Debug)]
14 15
 struct State {
15 16
     tag: Option<CurrentTag>,
16 17
     title: Option<String>,
... ...
@@ -24,22 +25,22 @@ impl State {
24 25
 
25 26
     pub fn with_title(self, title:String) -> Self {
26 27
         Self { title: Some(title.to_string()),
27
-               ..Default::default() }
28
+               ..self }
28 29
     }
29 30
 
30 31
     pub fn with_filename(self, filename: String) -> Self {
31 32
         Self { filename: Some(filename),
32
-               ..Default::default() }
33
+               ..self }
33 34
     }
34 35
 
35 36
     pub fn with_tag(self, tag:CurrentTag) -> Self {
36 37
         Self { tag: Some(tag),
37
-               ..Default::default() }
38
+               ..self }
38 39
     }
39 40
 
40 41
     pub fn remove_tag(self) -> Self {
41 42
         Self { tag: None,
42
-               ..Default::default() }
43
+               ..self }
43 44
     }
44 45
 }
45 46
 
... ...
@@ -65,22 +66,32 @@ fn main() {
65 66
     parser.feed_str(&contents);
66 67
     
67 68
     parser.fold(State::new(), {|state:State, element| {
69
+        println!("State: {:?}", state);
70
+
68 71
         match element.unwrap() {
69 72
             Event::ElementStart(tag) => {
73
+                println!("Start tag {}", tag.name);
74
+
70 75
                 match tag.name.as_ref() {
71 76
                     "title" => state.with_tag(CurrentTag::Title),
72 77
                     "data" => state.with_tag(CurrentTag::Data),
73
-                    "note" => State::new(),     // the start of a note resets everything
78
+                    "note" => {
79
+                        println!("Reset");
80
+                        State::new()     // the start of a note resets everything
81
+                    },
74 82
                     _ => state
75 83
                 }
76 84
             },
77 85
 
78
-            Event::ElementEnd(_) => {
86
+            Event::ElementEnd(tag) => {
79 87
                 // whatever tag we were following, it is not there anymore.
88
+                println!("Closing tag {}", tag.name);
80 89
                 state.remove_tag()
81 90
             },
82 91
 
83 92
             Event::Characters(data) => {
93
+                println!("Data");
94
+
84 95
                 match state.tag {
85 96
                     Some(CurrentTag::Title) => {
86 97
                         let slug = slugify(data);
... ...
@@ -88,8 +99,9 @@ fn main() {
88 99
                         std::fs::create_dir_all(Path::new(slug.as_str())).unwrap();
89 100
                         state.with_title(slug)
90 101
                     },
102
+
91 103
                     Some(CurrentTag::Data) => {
92
-                        let title = state.title.unwrap().to_string();
104
+                        let title = state.title.as_ref().unwrap().to_string();
93 105
                         let _filename = Path::new(&title);
94 106
 
95 107
                         state