optimization

This commit is contained in:
slonkazoid 2024-07-01 03:05:48 +03:00
parent bd093e7c20
commit 3623b61fbe
Signed by: slonk
SSH key fingerprint: SHA256:tbZfJX4IOvZ0LGWOWu5Ijo8jfMPi78TU7x1VoEeCIjM

View file

@ -27,25 +27,25 @@ pub struct AppState {
#[derive(Template)] #[derive(Template)]
#[template(path = "index.html")] #[template(path = "index.html")]
struct IndexTemplate { struct IndexTemplate<'a> {
title: String, title: &'a str,
description: String, description: &'a str,
posts: Vec<PostMetadata>, posts: Vec<PostMetadata>,
rss: bool, rss: bool,
df: DateFormat, df: &'a DateFormat,
js: bool, js: bool,
} }
#[derive(Template)] #[derive(Template)]
#[template(path = "post.html")] #[template(path = "post.html")]
struct PostTemplate { struct PostTemplate<'a> {
meta: PostMetadata, meta: &'a PostMetadata,
rendered: String, rendered: String,
rendered_in: RenderStats, rendered_in: RenderStats,
markdown_access: bool, markdown_access: bool,
df: DateFormat, df: &'a DateFormat,
js: bool, js: bool,
color: Option<String>, color: Option<&'a str>,
} }
#[derive(Deserialize)] #[derive(Deserialize)]
@ -55,22 +55,23 @@ struct QueryParams {
num_posts: Option<usize>, num_posts: Option<usize>,
} }
async fn index( async fn index<'a>(
State(AppState { config, posts }): State<AppState>, State(AppState { config, posts }): State<AppState>,
Query(query): Query<QueryParams>, Query(query): Query<QueryParams>,
) -> AppResult<IndexTemplate> { ) -> AppResult<Response> {
let posts = posts let posts = posts
.get_max_n_post_metadata_with_optional_tag_sorted(query.num_posts, query.tag.as_ref()) .get_max_n_post_metadata_with_optional_tag_sorted(query.num_posts, query.tag.as_ref())
.await?; .await?;
Ok(IndexTemplate { Ok(IndexTemplate {
title: config.title.clone(), title: &config.title,
description: config.description.clone(), description: &config.description,
posts, posts,
rss: config.rss.enable, rss: config.rss.enable,
df: config.date_format.clone(), df: &config.date_format,
js: config.js_enable, js: config.js_enable,
}) }
.into_response())
} }
async fn all_posts( async fn all_posts(
@ -148,24 +149,16 @@ async fn post(
Path(name): Path<String>, Path(name): Path<String>,
) -> AppResult<Response> { ) -> AppResult<Response> {
match posts.get_post(&name).await? { match posts.get_post(&name).await? {
ReturnedPost::Rendered(meta, rendered, rendered_in) => { ReturnedPost::Rendered(ref meta, rendered, rendered_in) => Ok(PostTemplate {
let color = meta
.color
.as_ref()
.or(config.default_color.as_ref())
.cloned();
let page = PostTemplate {
meta, meta,
rendered, rendered,
rendered_in, rendered_in,
markdown_access: config.markdown_access, markdown_access: config.markdown_access,
df: config.date_format.clone(), df: &config.date_format,
js: config.js_enable, js: config.js_enable,
color, color: meta.color.as_deref().or(config.default_color.as_deref()),
};
Ok(page.into_response())
} }
.into_response()),
ReturnedPost::Raw(body, content_type) => { ReturnedPost::Raw(body, content_type) => {
Ok(([(CONTENT_TYPE, content_type)], body).into_response()) Ok(([(CONTENT_TYPE, content_type)], body).into_response())
} }