Browse code

Simplified matches

Julio Biason authored on 28/02/2019 11:43:28
Showing 1 changed files

... ...
@@ -8,7 +8,12 @@ use slug::slugify;
8 8
 #[derive(Debug)]
9 9
 enum CurrentTag {
10 10
     Title,
11
-    Data
11
+    Data,
12
+    Content,
13
+    Resource,
14
+    ResourceData,
15
+    ResourceAttributes,
16
+    ResourceAttributesFilename
12 17
 }
13 18
 
14 19
 #[derive(Debug)]
... ...
@@ -44,6 +49,30 @@ impl State {
44 49
     }
45 50
 }
46 51
 
52
+fn create_note_storage(title: &str) -> String {
53
+    let slug = slugify(title);
54
+    println!("TITLE: {}", slug);
55
+    std::fs::create_dir_all(Path::new("data").join(slug.as_str())).unwrap();
56
+    slug
57
+}
58
+
59
+fn open_tag(current_state: State, tag: &str) -> State {
60
+    match tag {
61
+        "title" => current_state.with_tag(CurrentTag::Title),
62
+        "data" => current_state.with_tag(CurrentTag::Data),
63
+        "content" => current_state.with_tag(CurrentTag::Content),
64
+        "resource" => current_state.with_tag(CurrentTag::Resource),
65
+        "resource-attributes" => current_state.with_tag(CurrentTag::ResourceAttributes),
66
+        "file-name" => current_state.with_tag(CurrentTag::ResourceAttributesFilename),
67
+        "note" => State::new(),
68
+        _ => current_state
69
+    }
70
+}
71
+
72
+fn close_tag(current_state: State, _tag: &str) -> State {
73
+    current_state.remove_tag()
74
+}
75
+
47 76
 fn main() {
48 77
     let args:Vec<_> = env::args().collect();
49 78
     if args.len() != 2 {
... ...
@@ -69,49 +98,19 @@ fn main() {
69 98
         println!("State: {:?}", state);
70 99
 
71 100
         match element.unwrap() {
72
-            Event::ElementStart(tag) => {
73
-                println!("Start tag {}", tag.name);
74
-
75
-                match tag.name.as_ref() {
76
-                    "title" => state.with_tag(CurrentTag::Title),
77
-                    "data" => state.with_tag(CurrentTag::Data),
78
-                    "note" => {
79
-                        println!("Reset");
80
-                        State::new()     // the start of a note resets everything
81
-                    },
82
-                    _ => state
83
-                }
84
-            },
85
-
86
-            Event::ElementEnd(tag) => {
87
-                // whatever tag we were following, it is not there anymore.
88
-                println!("Closing tag {}", tag.name);
89
-                state.remove_tag()
90
-            },
91
-
101
+            Event::ElementStart(tag) => open_tag(state, tag.name.as_ref()),
102
+            Event::ElementEnd(tag) => close_tag(state, tag.name.as_ref()),
103
+            // Event::CDATA(_) => state,
92 104
             Event::Characters(data) => {
93 105
                 println!("Data");
94 106
 
95 107
                 match state.tag {
96
-                    Some(CurrentTag::Title) => {
97
-                        let slug = slugify(data);
98
-                        println!("TITLE: {}", slug);
99
-                        std::fs::create_dir_all(Path::new(slug.as_str())).unwrap();
100
-                        state.with_title(slug)
101
-                    },
102
-
103
-                    Some(CurrentTag::Data) => {
104
-                        let title = state.title.as_ref().unwrap().to_string();
105
-                        let _filename = Path::new(&title);
106
-
107
-                        state
108
-                    },
108
+                    Some(CurrentTag::Title) => state.with_title(create_note_storage(&data)),
109
+                    Some(CurrentTag::Data) => state,
110
+                    Some(CurrentTag::ResourceAttributesFilename) => state.with_filename(data),
109 111
                     _ => state
110 112
                 }
111 113
             },
112
-            Event::CDATA(_) => {
113
-                state
114
-            },
115 114
             _ => state
116 115
         }
117 116
     }});