From eea0cc764d42669c9bc0d6ce4ddad6c6be9be404 Mon Sep 17 00:00:00 2001 From: Adrian Hedqvist Date: Sat, 29 Jul 2023 12:04:37 +0200 Subject: [PATCH] cargo clippy -- -W clippy::pedantic --- src/handlers/mod.rs | 4 ++-- src/handlers/posts.rs | 30 ++++++++++++------------------ src/handlers/tags.rs | 16 ++++++---------- src/main.rs | 12 +++++++----- src/markdown.rs | 5 ++--- src/post.rs | 9 ++++----- src/tag.rs | 2 +- 7 files changed, 34 insertions(+), 44 deletions(-) diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 7d9ee85..4af681e 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -15,7 +15,7 @@ use lazy_static::lazy_static; use prometheus::{opts, Encoder, IntCounterVec, TextEncoder}; use std::sync::Arc; use tower_http::services::ServeFile; -use tracing::{instrument, log::*}; +use tracing::{instrument, log::{error, info}}; use crate::{AppState, WebsiteError}; @@ -111,7 +111,7 @@ pub async fn metrics_middleware(request: Request, next: Next) -> Respon response } -fn should_return_304(headers: HeaderMap, last_changed: Option>) -> bool { +fn should_return_304(headers: &HeaderMap, last_changed: Option>) -> bool { let Some(date) = last_changed else { info!("no last modified date"); return false; diff --git a/src/handlers/posts.rs b/src/handlers/posts.rs index 214acf2..7d94901 100644 --- a/src/handlers/posts.rs +++ b/src/handlers/posts.rs @@ -1,4 +1,4 @@ -use std::{sync::Arc}; +use std::sync::Arc; use axum::{ extract::{Path, State}, @@ -11,7 +11,7 @@ use hyper::{ HeaderMap, StatusCode, }; use serde_derive::Serialize; -use tracing::{instrument, log::*}; +use tracing::{instrument, log::warn}; use crate::{ post::{render_post, Post}, @@ -35,7 +35,7 @@ pub fn alias_router<'a>(posts: impl IntoIterator) -> Router Result { let mut posts: Vec<&Post> = state.posts.values().filter(|p| p.is_published()).collect(); - let last_changed = posts.iter().flat_map(|p| p.last_modified()).max(); + let last_changed = posts.iter().filter_map(|p| p.last_modified()).max(); - if should_return_304(headers, last_changed) { + if should_return_304(&headers, last_changed) { return Ok(StatusCode::NOT_MODIFIED.into_response()); } @@ -81,16 +81,14 @@ pub async fn index( let mut headers = vec![]; if let Some(date) = last_changed { - headers.push((header::LAST_MODIFIED, date.to_rfc2822())) + headers.push((header::LAST_MODIFIED, date.to_rfc2822())); } Ok(( StatusCode::OK, [( header::LAST_MODIFIED, - last_changed - .map(|d| d.to_rfc2822()) - .unwrap_or_else(|| chrono::offset::Utc::now().to_rfc2822()), + last_changed.map_or_else(|| chrono::offset::Utc::now().to_rfc2822(), |d| d.to_rfc2822()), )], Html(res), ) @@ -107,7 +105,7 @@ pub async fn view( let last_changed = post.last_modified(); - if should_return_304(headers, last_changed) { + if should_return_304(&headers, last_changed) { return Ok(StatusCode::NOT_MODIFIED.into_response()); } @@ -122,9 +120,7 @@ pub async fn view( StatusCode::OK, [( header::LAST_MODIFIED, - last_changed - .map(|d| d.to_rfc2822()) - .unwrap_or_else(|| chrono::offset::Utc::now().to_rfc2822()), + last_changed.map_or_else(|| chrono::offset::Utc::now().to_rfc2822(), |d| d.to_rfc2822()), )], Html(res), ) @@ -137,9 +133,9 @@ pub async fn feed( ) -> Result { let mut posts: Vec<&Post> = state.posts.values().filter(|p| p.is_published()).collect(); - let last_changed = posts.iter().flat_map(|p| p.last_modified()).max(); + let last_changed = posts.iter().filter_map(|p| p.last_modified()).max(); - if should_return_304(headers, last_changed) { + if should_return_304(&headers, last_changed) { return Ok(StatusCode::NOT_MODIFIED.into_response()); } @@ -153,9 +149,7 @@ pub async fn feed( (CONTENT_TYPE, "application/atom+xml"), ( header::LAST_MODIFIED, - &last_changed - .map(|d| d.to_rfc2822()) - .unwrap_or_else(|| chrono::offset::Utc::now().to_rfc2822()), + &last_changed.map_or_else(|| chrono::offset::Utc::now().to_rfc2822(), |d| d.to_rfc2822()), ), ], crate::feed::render_atom_feed(&state)?, diff --git a/src/handlers/tags.rs b/src/handlers/tags.rs index 04f5a5a..d2481c2 100644 --- a/src/handlers/tags.rs +++ b/src/handlers/tags.rs @@ -58,9 +58,9 @@ pub async fn view( .filter(|p| p.is_published() && p.tags.contains(&tag)) .collect(); - let last_changed = posts.iter().flat_map(|p| p.last_modified()).max(); + let last_changed = posts.iter().filter_map(|p| p.last_modified()).max(); - if should_return_304(headers, last_changed) { + if should_return_304(&headers, last_changed) { return Ok(StatusCode::NOT_MODIFIED.into_response()); } @@ -84,9 +84,7 @@ pub async fn view( [ ( header::LAST_MODIFIED, - &last_changed - .map(|d| d.to_rfc2822()) - .unwrap_or_else(|| chrono::offset::Utc::now().to_rfc2822()), + &last_changed.map_or_else(|| chrono::offset::Utc::now().to_rfc2822(), |d| d.to_rfc2822()), ) ], Html(res) @@ -107,9 +105,9 @@ pub async fn feed( .filter(|p| p.is_published() && p.tags.contains(&slug)) .collect(); - let last_changed = posts.iter().flat_map(|p| p.last_modified()).max(); + let last_changed = posts.iter().filter_map(|p| p.last_modified()).max(); - if should_return_304(headers, last_changed) { + if should_return_304(&headers, last_changed) { return Ok(StatusCode::NOT_MODIFIED.into_response()); } @@ -122,9 +120,7 @@ pub async fn feed( [(CONTENT_TYPE, "application/atom+xml"), ( header::LAST_MODIFIED, - &last_changed - .map(|d| d.to_rfc2822()) - .unwrap_or_else(|| chrono::offset::Utc::now().to_rfc2822()), + &last_changed.map_or_else(|| chrono::offset::Utc::now().to_rfc2822(), |d| d.to_rfc2822()), ) ], crate::feed::render_atom_tag_feed(tag, &state)?, diff --git a/src/main.rs b/src/main.rs index a106ffe..6e3af4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +#![warn(clippy::pedantic)] +#![allow(clippy::unused_async)] // axum handlers needs async, even if no awaiting happens use std::{collections::HashMap, fmt::Display, sync::Arc, time::Duration}; use axum::extract::MatchedPath; @@ -10,7 +12,8 @@ use post::Post; use tag::Tag; use tera::Tera; use tower_http::{compression::CompressionLayer, cors::CorsLayer}; -use tracing::{info_span, log::*, Span}; +use tracing::{field::Empty, info_span, log::info, Span}; + use tracing_subscriber::{prelude::*, EnvFilter}; mod feed; @@ -85,14 +88,13 @@ fn make_span(request: &Request) -> Span { let route = request .extensions() .get::() - .map(|mp| mp.as_str()) + .map(axum::extract::MatchedPath::as_str) .unwrap_or_default(); let method = request.method().as_str(); - let target = uri.path_and_query().map(|p| p.as_str()).unwrap_or_default(); + let target = uri.path_and_query().map(axum::http::uri::PathAndQuery::as_str).unwrap_or_default(); let name = format!("{method} {route}"); - use tracing::field::Empty; info_span!( "request", otel.name = %name, @@ -126,7 +128,7 @@ impl Display for WebsiteError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { WebsiteError::NotFound => write!(f, "Not found"), - _ => write!(f, "Internal error"), + WebsiteError::InternalError(e) => write!(f, "Internal error: {e}"), } } } diff --git a/src/markdown.rs b/src/markdown.rs index 3431043..5bbec22 100644 --- a/src/markdown.rs +++ b/src/markdown.rs @@ -1,11 +1,10 @@ -use color_eyre::Result; use pulldown_cmark::Event; use pulldown_cmark::Tag; use pulldown_cmark::{Options, Parser}; use crate::hilighting; -pub fn render_markdown_to_html(markdown: &str) -> Result { +pub fn render_markdown_to_html(markdown: &str) -> String { let options = Options::all(); let mut content_html = String::new(); let parser = Parser::new_ext(markdown, options); @@ -51,5 +50,5 @@ pub fn render_markdown_to_html(markdown: &str) -> Result { pulldown_cmark::html::push_html(&mut content_html, events.into_iter()); - Ok(content_html) + content_html } diff --git a/src/post.rs b/src/post.rs index c8db3f7..e4e7a57 100644 --- a/src/post.rs +++ b/src/post.rs @@ -9,7 +9,7 @@ use regex::Regex; use serde_derive::{Deserialize, Serialize}; use tokio::fs; -use tracing::{instrument, log::*}; +use tracing::{instrument, log::{debug, warn}}; use crate::{markdown, AppState, WebsiteError}; @@ -38,7 +38,7 @@ pub struct Post { impl Post { pub fn new(slug: String, content: String, fm: TomlFrontMatter) -> Post { Post { - absolute_path: format!("/posts/{}/", slug), + absolute_path: format!("/posts/{slug}/"), slug, content, title: fm.title, @@ -107,8 +107,7 @@ pub async fn load_post(slug: &str) -> color_eyre::eyre::Result { let tomlfm = tomlfm.expect("Missing frontmatter"); let content = content - .map(|c| markdown::render_markdown_to_html(&c)) - .transpose()?; + .map(|c| markdown::render_markdown_to_html(&c)); Ok(Post::new( slug.to_string(), @@ -144,7 +143,7 @@ pub async fn render_post(state: &AppState, post: &Post) -> Result(posts: impl IntoIterator) -> HashMap { let mut tags: HashMap = HashMap::new(); - for post in posts.into_iter() { + for post in posts { for key in &post.tags { if let Some(tag) = tags.get_mut(key) { tag.posts.push(post.slug.clone());