Browse code

Also capture the data (resource/data)

Julio Biason authored on 28/02/2019 12:22:25
Showing 1 changed files

... ...
@@ -3,12 +3,12 @@ use xml::{Event, Parser};
3 3
 use std::fs::File;
4 4
 use std::io::prelude::*;
5 5
 use std::path::Path;
6
+use std::vec::Vec;
6 7
 use slug::slugify;
7 8
 
8 9
 #[derive(Debug)]
9 10
 enum CurrentTag {
10 11
     Title,
11
-    Data,
12 12
     Content,
13 13
     Resource,
14 14
     ResourceData,
... ...
@@ -20,12 +20,13 @@ enum CurrentTag {
20 20
 struct State {
21 21
     tag: Option<CurrentTag>,
22 22
     title: Option<String>,
23
-    filename: Option<String>
23
+    filename: Option<String>,
24
+    data: Vec<u8>
24 25
 }
25 26
 
26 27
 impl State {
27 28
     pub fn new() -> Self {
28
-        Self { tag: None, title: None, filename: None }
29
+        Self { tag: None, title: None, filename: None, data: Vec::new() }
29 30
     }
30 31
 
31 32
     pub fn with_title(self, title:String) -> Self {
... ...
@@ -43,6 +44,11 @@ impl State {
43 44
                ..self }
44 45
     }
45 46
 
47
+    pub fn with_data(self, data:Vec<u8>) -> Self {
48
+        Self { data: data,
49
+               ..self }
50
+    }
51
+
46 52
     pub fn remove_tag(self) -> Self {
47 53
         Self { tag: None,
48 54
                ..self }
... ...
@@ -51,7 +57,6 @@ impl State {
51 57
 
52 58
 fn create_note_storage(title: &str) -> String {
53 59
     let slug = slugify(title);
54
-    println!("TITLE: {}", slug);
55 60
     std::fs::create_dir_all(Path::new("data").join(slug.as_str())).unwrap();
56 61
     slug
57 62
 }
... ...
@@ -59,7 +64,7 @@ fn create_note_storage(title: &str) -> String {
59 64
 fn open_tag(current_state: State, tag: &str) -> State {
60 65
     match tag {
61 66
         "title" => current_state.with_tag(CurrentTag::Title),
62
-        "data" => current_state.with_tag(CurrentTag::Data),
67
+        "data" => current_state.with_tag(CurrentTag::ResourceData),
63 68
         "content" => current_state.with_tag(CurrentTag::Content),
64 69
         "resource" => current_state.with_tag(CurrentTag::Resource),
65 70
         "resource-attributes" => current_state.with_tag(CurrentTag::ResourceAttributes),
... ...
@@ -100,13 +105,10 @@ fn main() {
100 105
         match element.unwrap() {
101 106
             Event::ElementStart(tag) => open_tag(state, tag.name.as_ref()),
102 107
             Event::ElementEnd(tag) => close_tag(state, tag.name.as_ref()),
103
-            // Event::CDATA(_) => state,
104 108
             Event::Characters(data) => {
105
-                println!("Data");
106
-
107 109
                 match state.tag {
108 110
                     Some(CurrentTag::Title) => state.with_title(create_note_storage(&data)),
109
-                    Some(CurrentTag::Data) => state,
111
+                    Some(CurrentTag::ResourceData) => state.with_data(data.into_bytes()),
110 112
                     Some(CurrentTag::ResourceAttributesFilename) => state.with_filename(data),
111 113
                     _ => state
112 114
                 }