diff options
Diffstat (limited to 'templates/src/lib.rs')
-rw-r--r-- | templates/src/lib.rs | 107 |
1 files changed, 43 insertions, 64 deletions
diff --git a/templates/src/lib.rs b/templates/src/lib.rs index d9fe86b..0f9f7c6 100644 --- a/templates/src/lib.rs +++ b/templates/src/lib.rs @@ -7,55 +7,9 @@ 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::StatusCode; - use axum::http::header::{CONTENT_TYPE, CONTENT_ENCODING, CACHE_CONTROL, X_CONTENT_TYPE_OPTIONS}; - - 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 { - let content_type: &'static str = if path.ends_with(".js") { - "application/javascript" - } else if path.ends_with(".css") { - "text/css" - } else if path.ends_with(".html") { - "text/html; charset=\"utf-8\"" - } else { - "application/octet-stream" - }; - - 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(), - 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() - } - } - } -} +pub mod assets; #[cfg(test)] mod tests { @@ -107,11 +61,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 +172,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 +190,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 +206,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 +219,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 +246,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 +258,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 +275,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 +313,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() { |