about summary refs log tree commit diff
path: root/kittybox-rs/templates/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'kittybox-rs/templates/src/lib.rs')
-rw-r--r--kittybox-rs/templates/src/lib.rs116
1 files changed, 49 insertions, 67 deletions
diff --git a/kittybox-rs/templates/src/lib.rs b/kittybox-rs/templates/src/lib.rs
index 39f1075..8e9abba 100644
--- a/kittybox-rs/templates/src/lib.rs
+++ b/kittybox-rs/templates/src/lib.rs
@@ -1,5 +1,5 @@
 mod templates;
-pub use templates::{ErrorPage, MainPage, Template, POSTS_PER_PAGE, Entry, VCard, Feed};
+pub use templates::{Entry, ErrorPage, Feed, MainPage, Template, VCard, POSTS_PER_PAGE};
 mod onboarding;
 pub use onboarding::OnboardingPage;
 mod login;
@@ -7,12 +7,12 @@ pub use login::LoginPage;
 
 #[cfg(test)]
 mod tests {
+    use faker_rand::en_us::internet::Domain;
     use faker_rand::lorem::Word;
-    use serde_json::json;
     use microformats::types::{Document, Item, PropertyValue, Url};
+    use serde_json::json;
     use std::cell::RefCell;
     use std::rc::Rc;
-    use faker_rand::en_us::internet::Domain;
 
     enum PostType {
         Note,
@@ -20,7 +20,7 @@ mod tests {
         ReplyTo(serde_json::Value),
         ReplyToLink(String),
         LikeOf(serde_json::Value),
-        LikeOfLink(String)
+        LikeOfLink(String),
     }
 
     fn gen_hcard(domain: &str) -> serde_json::Value {
@@ -36,7 +36,7 @@ mod tests {
             }
         })
     }
-    
+
     fn gen_random_post(domain: &str, kind: PostType) -> serde_json::Value {
         use faker_rand::lorem::{Paragraph, Sentence};
 
@@ -46,13 +46,14 @@ mod tests {
                 "value": content.to_string()
             })
         }
-            
+
         let uid = format!(
             "https://{domain}/posts/{}-{}-{}",
-            rand::random::<Word>(), rand::random::<Word>(), rand::random::<Word>()
+            rand::random::<Word>(),
+            rand::random::<Word>(),
+            rand::random::<Word>()
         );
-        let dt = chrono::offset::Local::now()
-            .to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
+        let dt = chrono::offset::Local::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true);
 
         match kind {
             PostType::Note => {
@@ -99,7 +100,7 @@ mod tests {
                         }]
                     }
                 })
-            },
+            }
             PostType::ReplyToLink(link) => {
                 let content = rand::random::<Paragraph>();
 
@@ -113,7 +114,7 @@ mod tests {
                         "in-reply-to": [link]
                     }
                 })
-            },
+            }
             PostType::LikeOf(ctx) => {
                 json!({
                     "type": ["h-entry"],
@@ -127,7 +128,7 @@ mod tests {
                         }]
                     }
                 })
-            },
+            }
             PostType::LikeOfLink(link) => {
                 json!({
                     "type": ["h-entry"],
@@ -142,20 +143,15 @@ mod tests {
         }
     }
 
-    fn check_dt_published(
-        mf2: &serde_json::Value,
-        item: &Rc<RefCell<Item>>
-    ) {
+    fn check_dt_published(mf2: &serde_json::Value, item: &Rc<RefCell<Item>>) {
         use microformats::types::temporal::Value as TemporalValue;
 
         let _item = item.borrow();
         let props = _item.properties.borrow();
         assert!(props.contains_key("published"));
 
-        if let Some(PropertyValue::Temporal(
-            TemporalValue::Timestamp(item)
-        )) = props.get("published")
-            .and_then(|v| v.first())
+        if let Some(PropertyValue::Temporal(TemporalValue::Timestamp(item))) =
+            props.get("published").and_then(|v| v.first())
         {
             use chrono::{DateTime, FixedOffset, NaiveDateTime};
 
@@ -171,8 +167,9 @@ mod tests {
             let dt = DateTime::<FixedOffset>::from_utc(ndt, offset);
 
             let expected: DateTime<FixedOffset> = chrono::DateTime::parse_from_rfc3339(
-                mf2["properties"]["published"][0].as_str().unwrap()
-            ).unwrap();
+                mf2["properties"]["published"][0].as_str().unwrap(),
+            )
+            .unwrap();
 
             assert_eq!(dt, expected);
         } else {
@@ -180,17 +177,12 @@ mod tests {
         }
     }
 
-    fn check_e_content(
-        mf2: &serde_json::Value,
-        item: &Rc<RefCell<Item>>
-    ) {
+    fn check_e_content(mf2: &serde_json::Value, item: &Rc<RefCell<Item>>) {
         let _item = item.borrow();
         let props = _item.properties.borrow();
         assert!(props.contains_key("content"));
 
-        if let Some(PropertyValue::Fragment(content)) =
-            props.get("content")
-            .and_then(|v| v.first())
+        if let Some(PropertyValue::Fragment(content)) = props.get("content").and_then(|v| v.first())
         {
             assert_eq!(
                 content.html,
@@ -199,7 +191,6 @@ mod tests {
         } else {
             unreachable!()
         }
-
     }
 
     #[test]
@@ -207,16 +198,12 @@ mod tests {
     fn test_note() {
         test_logger::ensure_env_logger_initialized();
 
-        let mf2 = gen_random_post(
-            &rand::random::<Domain>().to_string(),
-            PostType::Note
-        );
+        let mf2 = gen_random_post(&rand::random::<Domain>().to_string(), PostType::Note);
 
-        let html = crate::templates::Entry {
-            post: &mf2
-        }.to_string();
+        let html = crate::templates::Entry { post: &mf2 }.to_string();
 
-        let url: Url = mf2.pointer("/properties/uid/0")
+        let url: Url = mf2
+            .pointer("/properties/uid/0")
             .and_then(|i| i.as_str())
             .and_then(|u| u.parse().ok())
             .unwrap();
@@ -230,13 +217,13 @@ mod tests {
             check_dt_published(&mf2, &item);
             assert!(props.contains_key("uid"));
             assert!(props.contains_key("url"));
-            assert!(props.get("url")
-                    .unwrap()
-                    .iter()
-                    .any(|i| i == props.get("uid").and_then(|v| v.first()).unwrap()));
+            assert!(props
+                .get("url")
+                .unwrap()
+                .iter()
+                .any(|i| i == props.get("uid").and_then(|v| v.first()).unwrap()));
             // XXX: fails because of https://gitlab.com/maxburon/microformats-parser/-/issues/7
             assert!(!props.contains_key("name"));
-
         } else {
             unreachable!()
         }
@@ -246,14 +233,10 @@ mod tests {
     fn test_article() {
         test_logger::ensure_env_logger_initialized();
 
-        let mf2 = gen_random_post(
-            &rand::random::<Domain>().to_string(),
-            PostType::Article
-        );
-        let html = crate::templates::Entry {
-            post: &mf2
-        }.to_string();
-        let url: Url = mf2.pointer("/properties/uid/0")
+        let mf2 = gen_random_post(&rand::random::<Domain>().to_string(), PostType::Article);
+        let html = crate::templates::Entry { post: &mf2 }.to_string();
+        let url: Url = mf2
+            .pointer("/properties/uid/0")
             .and_then(|i| i.as_str())
             .and_then(|u| u.parse().ok())
             .unwrap();
@@ -267,10 +250,11 @@ mod tests {
             check_dt_published(&mf2, &item);
             assert!(props.contains_key("uid"));
             assert!(props.contains_key("url"));
-            assert!(props.get("url")
-                    .unwrap()
-                    .iter()
-                    .any(|i| i == props.get("uid").and_then(|v| v.first()).unwrap()));
+            assert!(props
+                .get("url")
+                .unwrap()
+                .iter()
+                .any(|i| i == props.get("uid").and_then(|v| v.first()).unwrap()));
             assert!(props.contains_key("name"));
             if let Some(PropertyValue::Plain(name)) = props.get("name").and_then(|v| v.first()) {
                 assert_eq!(
@@ -294,7 +278,7 @@ mod tests {
         for likeof in [
             PostType::LikeOf(gen_random_post(
                 &rand::random::<Domain>().to_string(),
-                PostType::Note
+                PostType::Note,
             )),
             PostType::LikeOfLink(format!(
                 "https://{}/posts/{}-{}-{}",
@@ -302,19 +286,15 @@ mod tests {
                 &rand::random::<Word>(),
                 &rand::random::<Word>(),
                 &rand::random::<Word>(),
-            ))
+            )),
         ] {
-            let mf2 = gen_random_post(
-                &rand::random::<Domain>().to_string(),
-                likeof
-            );
-            let url: Url = mf2.pointer("/properties/uid/0")
+            let mf2 = gen_random_post(&rand::random::<Domain>().to_string(), likeof);
+            let url: Url = mf2
+                .pointer("/properties/uid/0")
                 .and_then(|i| i.as_str())
                 .and_then(|u| u.parse().ok())
                 .unwrap();
-            let html = crate::templates::Entry {
-                post: &mf2
-            }.to_string();
+            let html = crate::templates::Entry { post: &mf2 }.to_string();
             let parsed: Document = microformats::from_html(&html, url.clone()).unwrap();
 
             if let Some(item) = parsed.items.get(0) {
@@ -329,7 +309,9 @@ mod tests {
                             url,
                             &mf2.pointer("/properties/like-of/0")
                                 .and_then(|i| i.as_str())
-                                .or_else(|| mf2.pointer("/properties/like-of/0/properties/uid/0").and_then(|i| i.as_str()))
+                                .or_else(|| mf2
+                                    .pointer("/properties/like-of/0/properties/uid/0")
+                                    .and_then(|i| i.as_str()))
                                 .and_then(|u| u.parse::<Url>().ok())
                                 .unwrap()
                         );
@@ -337,7 +319,7 @@ mod tests {
                     Some(PropertyValue::Item(_cite)) => {
                         todo!()
                     }
-                    other => panic!("Unexpected value in like-of: {:?}", other)
+                    other => panic!("Unexpected value in like-of: {:?}", other),
                 }
             } else {
                 unreachable!()