about summary refs log tree commit diff
path: root/kittybox-rs/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'kittybox-rs/src/lib.rs')
-rw-r--r--kittybox-rs/src/lib.rs52
1 files changed, 31 insertions, 21 deletions
diff --git a/kittybox-rs/src/lib.rs b/kittybox-rs/src/lib.rs
index 3f25689..a75c3ea 100644
--- a/kittybox-rs/src/lib.rs
+++ b/kittybox-rs/src/lib.rs
@@ -10,9 +10,10 @@ pub mod micropub;
 pub mod indieauth;
 
 pub mod companion {
+    use std::{collections::HashMap, sync::Arc};
     use axum::{
         extract::{Extension, Path},
-        response::IntoResponse
+        response::{IntoResponse, Response}
     };
 
     #[derive(Debug, Clone, Copy)]
@@ -21,45 +22,54 @@ pub mod companion {
         mime: &'static str
     }
 
-    type ResourceTable = std::sync::Arc<std::collections::HashMap<&'static str, Resource>>;
+    impl IntoResponse for &Resource {
+        fn into_response(self) -> Response {
+            (axum::http::StatusCode::OK,
+             [("Content-Type", self.mime)],
+             self.data).into_response()
+        }
+    }
+
+    // TODO replace with the "phf" crate someday
+    type ResourceTable = Arc<HashMap<&'static str, Resource>>;
 
     #[tracing::instrument]
     async fn map_to_static(
         Path(name): Path<String>,
         Extension(resources): Extension<ResourceTable>
-    ) -> impl IntoResponse {
+    ) -> Response {
         tracing::debug!("Searching for {} in the resource table...", name);
-        if let Some(res) = resources.get(name.as_str()) {
-            (axum::http::StatusCode::OK,
-             [("Content-Type", res.mime)],
-             res.data)
-        } else {
-            #[cfg(debug_assertions)]
-            tracing::error!("Not found");
-            (axum::http::StatusCode::NOT_FOUND,
-             [("Content-Type", "text/plain")],
-             "Not found. Sorry.".as_bytes())
+        match resources.get(name.as_str()) {
+            Some(res) => res.into_response(),
+            None => {
+                #[cfg(debug_assertions)] tracing::error!("Not found");
+
+                (axum::http::StatusCode::NOT_FOUND,
+                 [("Content-Type", "text/plain")],
+                 "Not found. Sorry.".as_bytes()).into_response()
+            }
         }
     }
 
     pub fn router() -> axum::Router {
-        let resources = {
-            let mut map = std::collections::HashMap::new();
+        let resources: ResourceTable = {
+            let mut map = HashMap::new();
 
             macro_rules! register_resource {
-                ($prefix:literal, ($filename:literal, $mime:literal)) => {{
-                    map.insert($filename, Resource {
+                ($map:ident, $prefix:literal, ($filename:literal, $mime:literal)) => {{
+                    $map.insert($filename, Resource {
                         data: include_bytes!(concat!($prefix, $filename)),
                         mime: $mime
                     })
                 }};
-                ($prefix:literal, ($filename:literal, $mime:literal), $( ($f:literal, $m:literal) ),+) => {{
-                    register_resource!($prefix, ($filename, $mime));
-                    register_resource!($prefix, $(($f, $m)),+);
+                ($map:ident, $prefix:literal, ($filename:literal, $mime:literal), $( ($f:literal, $m:literal) ),+) => {{
+                    register_resource!($map, $prefix, ($filename, $mime));
+                    register_resource!($map, $prefix, $(($f, $m)),+);
                 }};
             }
 
             register_resource! {
+                map,
                 "../companion-lite/",
                 ("index.html", "text/html; charset=\"utf-8\""),
                 ("main.js", "text/javascript"),
@@ -67,7 +77,7 @@ pub mod companion {
                 ("style.css", "text/css")
             };
 
-            std::sync::Arc::new(map)
+            Arc::new(map)
         };
 
         axum::Router::new()