Browse code

rustfmt + save the note content

Julio Biason authored on 14/03/2019 05:07:43
Showing 1 changed files

... ...
@@ -1,11 +1,10 @@
1
+use slug::slugify;
1 2
 use std::env;
2
-use xml::{Event, Parser};
3 3
 use std::fs::File;
4 4
 use std::io::prelude::*;
5 5
 use std::path::Path;
6 6
 use std::vec::Vec;
7
-use slug::slugify;
8
-use base64::decode;
7
+use xml::{Event, Parser};
9 8
 
10 9
 #[derive(Debug)]
11 10
 enum CurrentTag {
... ...
@@ -14,7 +13,7 @@ enum CurrentTag {
14 13
     Resource,
15 14
     ResourceData,
16 15
     ResourceAttributes,
17
-    ResourceAttributesFilename
16
+    ResourceAttributesFilename,
18 17
 }
19 18
 
20 19
 #[derive(Debug)]
... ...
@@ -22,42 +21,69 @@ struct State {
22 21
     tag: Option<CurrentTag>,
23 22
     title: Option<String>,
24 23
     filename: Option<String>,
25
-    data: Vec<u8>
24
+    data: Vec<u8>,
25
+    content: Vec<u8>,
26 26
 }
27 27
 
28 28
 impl State {
29 29
     pub fn new() -> Self {
30
-        Self { tag: None, title: None, filename: None, data: Vec::new() }
30
+        Self {
31
+            tag: None,
32
+            title: None,
33
+            filename: None,
34
+            data: Vec::new(),
35
+            content: Vec::new(),
36
+        }
31 37
     }
32 38
 
33
-    pub fn with_title(self, title:String) -> Self {
34
-        Self { title: Some(title.to_string()),
35
-               ..self }
39
+    pub fn with_title(self, title: String) -> Self {
40
+        Self {
41
+            title: Some(title.to_string()),
42
+            ..self
43
+        }
36 44
     }
37 45
 
38 46
     pub fn with_filename(self, filename: String) -> Self {
39
-        Self { filename: Some(filename),
40
-               ..self }
47
+        Self {
48
+            filename: Some(filename),
49
+            ..self
50
+        }
51
+    }
52
+
53
+    pub fn with_tag(self, tag: CurrentTag) -> Self {
54
+        Self {
55
+            tag: Some(tag),
56
+            ..self
57
+        }
41 58
     }
42 59
 
43
-    pub fn with_tag(self, tag:CurrentTag) -> Self {
44
-        Self { tag: Some(tag),
45
-               ..self }
60
+    pub fn with_data(self, data: Vec<u8>) -> Self {
61
+        Self { data: data, ..self }
46 62
     }
47 63
 
48
-    pub fn with_data(self, data:Vec<u8>) -> Self {
49
-        Self { data: data,
50
-               ..self }
64
+    pub fn with_content(self, content: Vec<u8>) -> Self {
65
+        Self {
66
+            content: content,
67
+            ..self
68
+        }
51 69
     }
52 70
 
53 71
     pub fn remove_tag(self) -> Self {
54
-        Self { tag: None,
55
-               ..self }
72
+        Self { tag: None, ..self }
56 73
     }
57 74
 
58 75
     pub fn remove_data(self) -> Self {
59
-        Self { data: Vec::new(),
60
-               ..self }
76
+        Self {
77
+            data: Vec::new(),
78
+            ..self
79
+        }
80
+    }
81
+
82
+    pub fn remove_content(self) -> Self {
83
+        Self {
84
+            content: Vec::new(),
85
+            ..self
86
+        }
61 87
     }
62 88
 }
63 89
 
... ...
@@ -76,7 +102,7 @@ fn open_tag(current_state: State, tag: &str) -> State {
76 102
         "resource-attributes" => current_state.with_tag(CurrentTag::ResourceAttributes),
77 103
         "file-name" => current_state.with_tag(CurrentTag::ResourceAttributesFilename),
78 104
         "note" => State::new(),
79
-        _ => current_state
105
+        _ => current_state,
80 106
     }
81 107
 }
82 108
 
... ...
@@ -93,59 +119,73 @@ fn dump_resource(current_state: &State) -> () {
93 119
     target.write_all(&content).unwrap();
94 120
 }
95 121
 
122
+fn dump_note_contents(current_state: &State) -> () {
123
+    let filename = Path::new("data")
124
+        .join(current_state.title.as_ref().unwrap())
125
+        .join("content.md");
126
+    let mut target = File::create(filename).unwrap();
127
+    target.write_all(&current_state.content).unwrap();
128
+}
129
+
96 130
 fn close_tag(current_state: State, tag: &str) -> State {
97 131
     match tag {
98 132
         "resource" => {
99 133
             dump_resource(&current_state);
100 134
             current_state.remove_data()
101 135
         },
102
-        _ => current_state.remove_tag()
136
+        "content" => {
137
+            dump_note_contents(&current_state);
138
+            current_state.remove_content()
139
+        },
140
+        _ => current_state.remove_tag(),
103 141
     }
104 142
 }
105 143
 
106 144
 fn main() {
107
-    let args:Vec<_> = env::args().collect();
145
+    let args: Vec<_> = env::args().collect();
108 146
     if args.len() != 2 {
109 147
         println!("Required: filename.");
110 148
         std::process::exit(2);
111 149
     }
112
-    
150
+
113 151
     let filename = &args[1];
114
-        
152
+
115 153
     println!("Will process {}", filename);
116
-    
154
+
117 155
     // Need to find a way to load small chunks and feed it to the parser while parsing.
118 156
     // (E.g., load 1024 bytes, feed it to the parser and, if the parser can't continue,
119 157
     //  feed more data, till the end of file).
120 158
     let mut file = File::open(filename).unwrap();
121 159
     let mut contents = String::new();
122 160
     file.read_to_string(&mut contents).unwrap();
123
-        
161
+
124 162
     let mut parser = Parser::new();
125 163
     parser.feed_str(&contents);
126
-    
127
-    parser.fold(State::new(), {|state:State, element| {
128
-        println!("State: {:?}", state);
129
-
130
-        match element.unwrap() {
131
-            Event::ElementStart(tag) => open_tag(state, tag.name.as_ref()),
132
-            Event::ElementEnd(tag) => close_tag(state, tag.name.as_ref()),
133
-            Event::Characters(data) => {
134
-                match state.tag {
135
-                    Some(CurrentTag::Title) => state.with_title(create_note_storage(&data)),
136
-                    Some(CurrentTag::ResourceData) => state.with_data(
137
-                        data
138
-                            .into_bytes()
139
-                            .iter()
140
-                            .filter(|&x| *x != 13 && *x != 10)  // remove new lines, it is base 64, after all
141
-                            .map(|&x| x)
142
-                            .collect()
143
-                    ),
144
-                    Some(CurrentTag::ResourceAttributesFilename) => state.with_filename(data),
145
-                    _ => state
164
+
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
+                    }
146 186
                 }
147
-            },
148
-            _ => state
187
+                _ => state,
188
+            }
149 189
         }
150
-    }});
190
+    });
151 191
 }