Browse Source

Refactor highlighting

master
Dylan Baker 5 years ago
parent
commit
bc9873c918
1 changed files with 27 additions and 29 deletions
  1. 27
    29
      src/routes.rs

+ 27
- 29
src/routes.rs View File

@@ -19,6 +19,22 @@ fn error_response(error: Error) -> Status {
19 19
     }
20 20
 }
21 21
 
22
+fn format_gist(filetype: Option<String>, body: String) -> String {
23
+    filetype.map_or(
24
+        format!("<pre class='plaintext'>{}</pre>", body),
25
+        |filetype| {
26
+            let ps = SyntaxSet::load_defaults_newlines();
27
+            let ts = ThemeSet::load_defaults();
28
+            ps.find_syntax_by_extension(&filetype).map_or(
29
+                format!("<pre class='plaintext'>{}</pre>", body),
30
+                |syntax| {
31
+                    highlighted_html_for_string(&body, &ps, syntax, &ts.themes["Solarized (light)"])
32
+                },
33
+            )
34
+        },
35
+    )
36
+}
37
+
22 38
 #[get("/")]
23 39
 pub fn index(connection: DbConn) -> Result<Json<Vec<Gist>>, Status> {
24 40
     gists::all(&connection)
@@ -28,40 +44,22 @@ pub fn index(connection: DbConn) -> Result<Json<Vec<Gist>>, Status> {
28 44
 
29 45
 #[get("/gists/<id>")]
30 46
 pub fn show_gist(id: i32, connection: DbConn) -> Template {
31
-    let gist = match gists::get(&connection, id) {
47
+    let result = match gists::get(&connection, id) {
32 48
         Ok(gist) => Some(gist),
33 49
         Err(_) => None,
34 50
     };
35
-    let mut context = HashMap::new();
36 51
 
37
-    match gist {
38
-        Some(g) => {
39
-            let body = match g.filetype {
40
-                Some(filetype) => {
41
-                    let ps = SyntaxSet::load_defaults_newlines();
42
-                    let ts = ThemeSet::load_defaults();
43
-                    let syntax = ps.find_syntax_by_extension(&filetype);
52
+    let (title, body) = result.map_or(
53
+        (
54
+            String::from("404 - Gist not found"),
55
+            format!("<h3'>{}</h3>", "Gist not found"),
56
+        ),
57
+        |gist| (gist.title, format_gist(gist.filetype, gist.body)),
58
+    );
44 59
 
45
-                    match syntax {
46
-                        Some(s) => highlighted_html_for_string(
47
-                            &g.body,
48
-                            &ps,
49
-                            s,
50
-                            &ts.themes["Solarized (light)"],
51
-                        ),
52
-                        None => format!("<pre class='plaintext'>{}</pre>", g.body),
53
-                    }
54
-                }
55
-                None => format!("<pre class='plaintext'>{}</pre>", g.body),
56
-            };
57
-            context.insert("title", g.title.clone());
58
-            context.insert("body", body);
59
-        }
60
-        None => {
61
-            context.insert("title", "404".to_string());
62
-            context.insert("body", "<h3>Gist not found</h3>".to_string());
63
-        }
64
-    }
60
+    let mut context: HashMap<&str, String> = HashMap::new();
61
+    context.insert("title", title);
62
+    context.insert("body", body);
65 63
 
66 64
     Template::render("gists/show", &context)
67 65
 }

Loading…
Cancel
Save