about summary refs log tree commit diff
path: root/templates
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2022-05-23 17:45:38 +0300
committerVika <vika@fireburn.ru>2022-05-23 18:22:07 +0300
commit067d6c4c08120bb9b93190911bd00adf9159bba1 (patch)
treeada0b00b201b10faeae524e176c1d7c5f7cc2922 /templates
parent32317404bf47ac9db34f954ab74a9e400dc32037 (diff)
downloadkittybox-067d6c4c08120bb9b93190911bd00adf9159bba1.tar.zst
templates: simplify logic
There were lots of unneccesary Option::unwrap() invocations that could
be replaced with `if let` statements. This makes the code cleaner and
less likely to panic in case a corrupted, incomplete or manually
injected MF2-JSON document needs to be rendered.
Diffstat (limited to 'templates')
-rw-r--r--templates/src/templates.rs53
1 files changed, 29 insertions, 24 deletions
diff --git a/templates/src/templates.rs b/templates/src/templates.rs
index 44e6042..f2040fc 100644
--- a/templates/src/templates.rs
+++ b/templates/src/templates.rs
@@ -79,29 +79,28 @@ markup::define! {
     Entry<'a>(post: &'a serde_json::Value) {
         article."h-entry" {
             header.metadata {
-                @if post["properties"]["name"][0].is_string() {
-                    h1."p-name" {
-                        @post["properties"]["name"][0].as_str().unwrap()
-                    }
-                } else {
-                    @if post["properties"]["author"][0].is_object() {
-                        section."mini-h-card" {
-                            a.larger[href=post["properties"]["author"][0]["properties"]["uid"][0].as_str().unwrap()] {
-                                @if post["properties"]["author"][0]["properties"]["photo"][0].is_string() {
-                                    img[src=post["properties"]["author"][0]["properties"]["photo"][0].as_str().unwrap()] {}
-                                }
-                                @post["properties"]["author"][0]["properties"]["name"][0].as_str().unwrap()
+                @if let Some(name) = post["properties"]["name"][0].as_str() {
+                    h1."p-name" { @name }
+                }
+                @if let Some(author) = post["properties"]["author"][0].as_object() {
+                    section."mini-h-card" {
+                        a.larger."u-author"[href=author["properties"]["uid"][0].as_str().unwrap()] {
+                            @if let Some(photo) = author["properties"]["photo"][0].as_str() {
+                                img[src=photo, loading="lazy"];
                             }
+                            @author["properties"]["name"][0].as_str().unwrap()
                         }
                     }
                 }
                 div {
                     span {
                         a."u-url"."u-uid"[href=post["properties"]["uid"][0].as_str().unwrap()] {
-                            time."dt-published"[datetime=post["properties"]["published"][0].as_str().unwrap()] {
-                                @chrono::DateTime::parse_from_rfc3339(post["properties"]["published"][0].as_str().unwrap())
-                                    .map(|dt| dt.format("%a %b %e %T %Y").to_string())
-                                    .unwrap_or("ERROR: Couldn't parse the datetime".to_string())
+                            @if let Some(published) = post["properties"]["published"][0].as_str() {
+                                time."dt-published"[datetime=published] {
+                                    @chrono::DateTime::parse_from_rfc3339(published)
+                                        .map(|dt| dt.format("%a %b %e %T %Y").to_string())
+                                        .unwrap_or("sometime in the past".to_string())
+                                }
                             }
                         }
                     }
@@ -239,17 +238,23 @@ markup::define! {
         }
     }
     PhotoGallery<'a>(photos: Option<&'a Vec<serde_json::Value>>) {
-        @if photos.is_some() {
-            @for photo in photos.unwrap() {
-                @if photo.is_string() {
-                    img."u-photo"[src=photo.as_str().unwrap(), loading="lazy"];
-                } else if photo.is_array() {
-                    @if photo["thumbnail"].is_string() {
+        @if let Some(photos) = photos {
+            @for photo in photos.iter() {
+                @if let Some(photo) = photo.as_str() {
+                    img."u-photo"[src=photo, loading="lazy"];
+                } else if photo.is_object() {
+                    @if let Some(thumbnail) = photo["thumbnail"].as_str() {
                         a."u-photo"[href=photo["value"].as_str().unwrap()] {
-                            img[src=photo["thumbnail"].as_str().unwrap(), loading="lazy", alt=photo["alt"].as_str().unwrap_or("")];
+                            img[src=thumbnail,
+                                loading="lazy",
+                                alt=photo["alt"].as_str().unwrap_or("")
+                            ];
                         }
                     } else {
-                        img."u-photo"[src=photo["value"].as_str().unwrap(), loading="lazy", alt=photo["alt"].as_str().unwrap_or("")];
+                        img."u-photo"[src=photo["value"].as_str().unwrap(),
+                                      loading="lazy",
+                                      alt=photo["alt"].as_str().unwrap_or("")
+                        ];
                     }
                 }
             }