Browse code

Starting to deal with data

Julio Biason authored on 28/02/2019 08:41:00
Showing 1 changed files

... ...
@@ -2,30 +2,37 @@ use std::env;
2 2
 use xml::{Event, Parser};
3 3
 use std::fs::File;
4 4
 use std::io::prelude::*;
5
+use std::path::Path;
5 6
 use slug::slugify;
6 7
 
7 8
 enum CurrentTag {
8
-    Title
9
+    Title,
10
+    Data
9 11
 }
10 12
 
11 13
 #[derive(Default)]
12 14
 struct State {
13 15
     tag: Option<CurrentTag>,
14
-    title: Option<String>
16
+    title: Option<String>,
17
+    filename: Option<String>
15 18
 }
16 19
 
17 20
 impl State {
18 21
     pub fn new() -> Self {
19
-        Self { tag: None, title: None }
22
+        Self { tag: None, title: None, filename: None }
20 23
     }
21 24
 
22
-    pub fn change_title(self, title:&str) -> Self {
25
+    pub fn with_title(self, title:String) -> Self {
23 26
         Self { title: Some(title.to_string()),
24
-               ..Default::default()
25
-         }
27
+               ..Default::default() }
28
+    }
29
+
30
+    pub fn with_filename(self, filename: String) -> Self {
31
+        Self { filename: Some(filename),
32
+               ..Default::default() }
26 33
     }
27 34
 
28
-    pub fn change_tag(self, tag:CurrentTag) -> Self {
35
+    pub fn with_tag(self, tag:CurrentTag) -> Self {
29 36
         Self { tag: Some(tag),
30 37
                ..Default::default() }
31 38
     }
... ...
@@ -61,21 +68,31 @@ fn main() {
61 68
         match element.unwrap() {
62 69
             Event::ElementStart(tag) => {
63 70
                 match tag.name.as_ref() {
64
-                    "title" => state.change_tag(CurrentTag::Title),
71
+                    "title" => state.with_tag(CurrentTag::Title),
72
+                    "data" => state.with_tag(CurrentTag::Data),
65 73
                     "note" => State::new(),     // the start of a note resets everything
66 74
                     _ => state
67 75
                 }
68 76
             },
77
+
69 78
             Event::ElementEnd(_) => {
79
+                // whatever tag we were following, it is not there anymore.
70 80
                 state.remove_tag()
71 81
             },
82
+
72 83
             Event::Characters(data) => {
73
-                // println!("Data: {}", data);
74 84
                 match state.tag {
75 85
                     Some(CurrentTag::Title) => {
76
-                        println!("TITLE: {}", slugify(&data));
77
-                        let new_state = state.change_title(&slugify(&data));
78
-                        new_state
86
+                        let slug = slugify(data);
87
+                        println!("TITLE: {}", slug);
88
+                        std::fs::create_dir_all(Path::new(slug.as_str())).unwrap();
89
+                        state.with_title(slug)
90
+                    },
91
+                    Some(CurrentTag::Data) => {
92
+                        let title = state.title.unwrap().to_string();
93
+                        let _filename = Path::new(&title);
94
+
95
+                        state
79 96
                     },
80 97
                     _ => state
81 98
                 }