Преглед на файлове

Refactor highlighting

master
Dylan Baker преди 5 години
родител
ревизия
bc9873c918
променени са 1 файла, в които са добавени 27 реда и са изтрити 29 реда
  1. 27
    29
      src/routes.rs

+ 27
- 29
src/routes.rs Целия файл

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
 #[get("/")]
38
 #[get("/")]
23
 pub fn index(connection: DbConn) -> Result<Json<Vec<Gist>>, Status> {
39
 pub fn index(connection: DbConn) -> Result<Json<Vec<Gist>>, Status> {
24
     gists::all(&connection)
40
     gists::all(&connection)
28
 
44
 
29
 #[get("/gists/<id>")]
45
 #[get("/gists/<id>")]
30
 pub fn show_gist(id: i32, connection: DbConn) -> Template {
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
         Ok(gist) => Some(gist),
48
         Ok(gist) => Some(gist),
33
         Err(_) => None,
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
     Template::render("gists/show", &context)
64
     Template::render("gists/show", &context)
67
 }
65
 }

Loading…
Отказ
Запис