Browse code

Instead of going to unwrap() directly, capture the Event

This way, we can actually slowly feed the parser with content and it
will slowly process the elements.

Julio Biason authored on 14/03/2019 06:01:16
Showing 1 changed files

... ...
@@ -141,6 +141,29 @@ fn close_tag(current_state: State, tag: &str) -> State {
141 141
     }
142 142
 }
143 143
 
144
+fn parse_element(element:Event, state: State) -> State {
145
+    match element {
146
+        Event::ElementStart(tag) => open_tag(state, tag.name.as_ref()),
147
+        Event::ElementEnd(tag) => close_tag(state, tag.name.as_ref()),
148
+        Event::Characters(data) => {
149
+            match state.tag {
150
+                Some(CurrentTag::Title) => state.with_title(create_note_storage(&data)),
151
+                Some(CurrentTag::ResourceData) => state.with_data(
152
+                    data.into_bytes()
153
+                    .iter()
154
+                    .filter(|&x| *x != 13 && *x != 10) // remove new lines, it is base 64, after all
155
+                    .map(|&x| x)
156
+                    .collect(),
157
+                    ),
158
+                Some(CurrentTag::Content) => state.with_content(data.into_bytes()),
159
+                Some(CurrentTag::ResourceAttributesFilename) => state.with_filename(data),
160
+                _ => state,
161
+            }
162
+        }
163
+        _ => state,
164
+    }
165
+}
166
+
144 167
 fn main() {
145 168
     let args: Vec<_> = env::args().collect();
146 169
     if args.len() != 2 {
... ...
@@ -162,30 +185,12 @@ fn main() {
162 185
     let mut parser = Parser::new();
163 186
     parser.feed_str(&contents);
164 187
 
165
-    parser.fold(State::new(), {
166
-        |state: State, element| {
167
-            println!("State: {:?}", state);
168
-
169
-            match element.unwrap() {
170
-                Event::ElementStart(tag) => open_tag(state, tag.name.as_ref()),
171
-                Event::ElementEnd(tag) => close_tag(state, tag.name.as_ref()),
172
-                Event::Characters(data) => {
173
-                    match state.tag {
174
-                        Some(CurrentTag::Title) => state.with_title(create_note_storage(&data)),
175
-                        Some(CurrentTag::ResourceData) => state.with_data(
176
-                            data.into_bytes()
177
-                                .iter()
178
-                                .filter(|&x| *x != 13 && *x != 10) // remove new lines, it is base 64, after all
179
-                                .map(|&x| x)
180
-                                .collect(),
181
-                        ),
182
-                        Some(CurrentTag::Content) => state.with_content(data.into_bytes()),
183
-                        Some(CurrentTag::ResourceAttributesFilename) => state.with_filename(data),
184
-                        _ => state,
185
-                    }
186
-                }
187
-                _ => state,
188
-            }
188
+    parser.fold(State::new(), { |state: State, parsing_result| {
189
+        println!("State: {:?}", state);
190
+
191
+        match parsing_result {
192
+            Ok(element) => parse_element(element, state),
193
+            Err(_) => state,
189 194
         }
190
-    });
195
+    }});
191 196
 }