about summary refs log tree commit diff
path: root/templates/src/lib.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2025-04-09 23:31:02 +0300
committerVika <vika@fireburn.ru>2025-04-09 23:31:57 +0300
commit8826d9446e6c492db2243b9921e59ce496027bef (patch)
tree63738aa9001cb73b11cb0e974e93129bcdf1adbb /templates/src/lib.rs
parent519cadfbb298f50cbf819dde757037ab56e2863e (diff)
downloadkittybox-8826d9446e6c492db2243b9921e59ce496027bef.tar.zst
cargo fmt
Change-Id: I80e81ebba3f0cdf8c094451c9fe3ee4126b8c888
Diffstat (limited to 'templates/src/lib.rs')
-rw-r--r--templates/src/lib.rs109
1 files changed, 70 insertions, 39 deletions
diff --git a/templates/src/lib.rs b/templates/src/lib.rs
index d9fe86b..fde0dab 100644
--- a/templates/src/lib.rs
+++ b/templates/src/lib.rs
@@ -7,23 +7,23 @@ pub use indieauth::AuthorizationRequestPage;
 mod login;
 pub use login::{LoginPage, LogoutPage};
 mod mf2;
-pub use mf2::{Entry, VCard, Feed, Food, POSTS_PER_PAGE};
+pub use mf2::{Entry, Feed, Food, VCard, POSTS_PER_PAGE};
 
 pub mod admin;
 
 pub mod assets {
-    use axum::response::{IntoResponse, Response};
     use axum::extract::Path;
+    use axum::http::header::{
+        CACHE_CONTROL, CONTENT_ENCODING, CONTENT_TYPE, X_CONTENT_TYPE_OPTIONS,
+    };
     use axum::http::StatusCode;
-    use axum::http::header::{CONTENT_TYPE, CONTENT_ENCODING, CACHE_CONTROL, X_CONTENT_TYPE_OPTIONS};
+    use axum::response::{IntoResponse, Response};
 
     const ASSETS: include_dir::Dir<'static> = include_dir::include_dir!("$OUT_DIR/");
     const CACHE_FOR_A_DAY: &str = "max-age=86400";
     const GZIP: &str = "gzip";
 
-    pub async fn statics(
-        Path(path): Path<String>
-    ) -> Response {
+    pub async fn statics(Path(path): Path<String>) -> Response {
         let content_type: &'static str = if path.ends_with(".js") {
             "application/javascript"
         } else if path.ends_with(".css") {
@@ -35,24 +35,30 @@ pub mod assets {
         };
 
         match ASSETS.get_file(path.clone() + ".gz") {
-            Some(file) => (StatusCode::OK,
-                           [
-                               (CONTENT_TYPE, content_type),
-                               (CONTENT_ENCODING, GZIP),
-                               (CACHE_CONTROL, CACHE_FOR_A_DAY),
-                               (X_CONTENT_TYPE_OPTIONS, "nosniff")
-                           ],
-                           file.contents()).into_response(),
+            Some(file) => (
+                StatusCode::OK,
+                [
+                    (CONTENT_TYPE, content_type),
+                    (CONTENT_ENCODING, GZIP),
+                    (CACHE_CONTROL, CACHE_FOR_A_DAY),
+                    (X_CONTENT_TYPE_OPTIONS, "nosniff"),
+                ],
+                file.contents(),
+            )
+                .into_response(),
             None => match ASSETS.get_file(path) {
-                Some(file) => (StatusCode::OK,
-                               [
-                                   (CONTENT_TYPE, content_type),
-                                   (CACHE_CONTROL, CACHE_FOR_A_DAY),
-                                   (X_CONTENT_TYPE_OPTIONS, "nosniff")
-                               ],
-                               file.contents()).into_response(),
-                None => StatusCode::NOT_FOUND.into_response()
-            }
+                Some(file) => (
+                    StatusCode::OK,
+                    [
+                        (CONTENT_TYPE, content_type),
+                        (CACHE_CONTROL, CACHE_FOR_A_DAY),
+                        (X_CONTENT_TYPE_OPTIONS, "nosniff"),
+                    ],
+                    file.contents(),
+                )
+                    .into_response(),
+                None => StatusCode::NOT_FOUND.into_response(),
+            },
         }
     }
 }
@@ -107,11 +113,11 @@ mod tests {
         let dt = time::OffsetDateTime::now_utc()
             .to_offset(
                 time::UtcOffset::from_hms(
-                    rand::distributions::Uniform::new(-11, 12)
-                        .sample(&mut rand::thread_rng()),
+                    rand::distributions::Uniform::new(-11, 12).sample(&mut rand::thread_rng()),
                     if rand::random::<bool>() { 0 } else { 30 },
-                    0
-                ).unwrap()
+                    0,
+                )
+                .unwrap(),
             )
             .format(&time::format_description::well_known::Rfc3339)
             .unwrap();
@@ -218,14 +224,15 @@ mod tests {
             // potentially with an offset?
             let offset = item.as_offset().unwrap().data;
             let date = item.as_date().unwrap().data;
-            let time =  item.as_time().unwrap().data;
+            let time = item.as_time().unwrap().data;
 
             let dt = date.with_time(time).assume_offset(offset);
             let expected = time::OffsetDateTime::parse(
                 mf2["properties"]["published"][0].as_str().unwrap(),
-                &time::format_description::well_known::Rfc3339
-            ).unwrap();
-            
+                &time::format_description::well_known::Rfc3339,
+            )
+            .unwrap();
+
             assert_eq!(dt, expected);
         } else {
             unreachable!()
@@ -235,7 +242,8 @@ mod tests {
     fn check_e_content(mf2: &serde_json::Value, item: &Item) {
         assert!(item.properties.contains_key("content"));
 
-        if let Some(PropertyValue::Fragment(content)) = item.properties.get("content").and_then(|v| v.first())
+        if let Some(PropertyValue::Fragment(content)) =
+            item.properties.get("content").and_then(|v| v.first())
         {
             assert_eq!(
                 content.html,
@@ -250,7 +258,11 @@ mod tests {
     fn test_note() {
         let mf2 = gen_random_post(&rand::random::<Domain>().to_string(), PostType::Note);
 
-        let html = crate::mf2::Entry { post: &mf2, from_feed: false, }.to_string();
+        let html = crate::mf2::Entry {
+            post: &mf2,
+            from_feed: false,
+        }
+        .to_string();
 
         let url: Url = mf2
             .pointer("/properties/uid/0")
@@ -259,7 +271,12 @@ mod tests {
             .unwrap();
         let parsed: Document = microformats::from_html(&html, url.clone()).unwrap();
 
-        if let Some(item) = parsed.into_iter().find(|i| i.properties.get("url").unwrap().contains(&PropertyValue::Url(url.clone()))) {
+        if let Some(item) = parsed.into_iter().find(|i| {
+            i.properties
+                .get("url")
+                .unwrap()
+                .contains(&PropertyValue::Url(url.clone()))
+        }) {
             let props = &item.properties;
 
             check_e_content(&mf2, &item);
@@ -281,7 +298,11 @@ mod tests {
     #[test]
     fn test_article() {
         let mf2 = gen_random_post(&rand::random::<Domain>().to_string(), PostType::Article);
-        let html = crate::mf2::Entry { post: &mf2, from_feed: false, }.to_string();
+        let html = crate::mf2::Entry {
+            post: &mf2,
+            from_feed: false,
+        }
+        .to_string();
         let url: Url = mf2
             .pointer("/properties/uid/0")
             .and_then(|i| i.as_str())
@@ -289,8 +310,12 @@ mod tests {
             .unwrap();
         let parsed: Document = microformats::from_html(&html, url.clone()).unwrap();
 
-        if let Some(item) = parsed.into_iter().find(|i| i.properties.get("url").unwrap().contains(&PropertyValue::Url(url.clone()))) {
-
+        if let Some(item) = parsed.into_iter().find(|i| {
+            i.properties
+                .get("url")
+                .unwrap()
+                .contains(&PropertyValue::Url(url.clone()))
+        }) {
             check_e_content(&mf2, &item);
             check_dt_published(&mf2, &item);
             assert!(item.properties.contains_key("uid"));
@@ -302,7 +327,9 @@ mod tests {
                 .iter()
                 .any(|i| i == item.properties.get("uid").and_then(|v| v.first()).unwrap()));
             assert!(item.properties.contains_key("name"));
-            if let Some(PropertyValue::Plain(name)) = item.properties.get("name").and_then(|v| v.first()) {
+            if let Some(PropertyValue::Plain(name)) =
+                item.properties.get("name").and_then(|v| v.first())
+            {
                 assert_eq!(
                     name,
                     mf2.pointer("/properties/name/0")
@@ -338,7 +365,11 @@ mod tests {
                 .and_then(|i| i.as_str())
                 .and_then(|u| u.parse().ok())
                 .unwrap();
-            let html = crate::mf2::Entry { post: &mf2, from_feed: false, }.to_string();
+            let html = crate::mf2::Entry {
+                post: &mf2,
+                from_feed: false,
+            }
+            .to_string();
             let parsed: Document = microformats::from_html(&html, url.clone()).unwrap();
 
             if let Some(item) = parsed.items.first() {