From b7523c22f3d40511ab7bb77967fbcd75863d20b9 Mon Sep 17 00:00:00 2001 From: Matt Kantor Date: Mon, 30 Jun 2025 17:02:39 -0400 Subject: [PATCH 1/2] Switch to Rust 2024 edition. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 35da975..8ef778a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ categories = ["web-programming::http-server", "command-line-utilities"] keywords = ["web", "server", "http", "templating", "handlebars"] readme = "README.md" license = "GPL-3.0" -edition = "2021" +edition = "2024" include = [ "src/**/*", "Cargo.toml", From e5ac196e02de7262406197cb67cd74ae5449d94e Mon Sep 17 00:00:00 2001 From: Matt Kantor Date: Tue, 1 Jul 2025 08:48:43 -0400 Subject: [PATCH 2/2] Run `cargo fmt`. --- benches/load_test.rs | 4 +- src/cli.rs | 32 +++------- src/content/body.rs | 2 +- src/content/content_directory.rs | 2 +- src/content/content_engine.rs | 88 +++++++++------------------ src/content/content_index.rs | 2 +- src/content/content_item.rs | 2 +- src/content/content_registry.rs | 2 +- src/content/handlebars_helpers/get.rs | 12 ++-- src/http.rs | 7 ++- tests/integration_tests.rs | 2 +- tests/lib/mod.rs | 14 +++-- 12 files changed, 68 insertions(+), 101 deletions(-) diff --git a/benches/load_test.rs b/benches/load_test.rs index 297103c..923ae76 100644 --- a/benches/load_test.rs +++ b/benches/load_test.rs @@ -4,9 +4,9 @@ use actix_web::error::PayloadError; use actix_web::http::StatusCode; use actix_web::test::unused_addr; use bytes::{Bytes, BytesMut}; -use criterion::{criterion_main, BenchmarkId, Criterion}; +use criterion::{BenchmarkId, Criterion, criterion_main}; use futures::FutureExt; -use futures::{future, Stream, TryStreamExt}; +use futures::{Stream, TryStreamExt, future}; use lazy_static::lazy_static; use mime_guess::MimeGuess; use operator::content::ContentDirectory; diff --git a/src/cli.rs b/src/cli.rs index dde37a5..202ad11 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -257,12 +257,9 @@ mod tests { ); let output_as_str = str::from_utf8(output.as_slice()).expect("Output was not UTF-8"); assert_eq!( - output_as_str, - expected_output, + output_as_str, expected_output, "Template rendering for `{}` did not produce the expected output (\"{}\"), instead got \"{}\"", - template, - expected_output, - output_as_str + template, expected_output, output_as_str ); } } @@ -303,13 +300,10 @@ mod tests { ); let output_as_str = str::from_utf8(output.as_slice()).expect("Output was not UTF-8"); assert_eq!( - output_as_str, - expected_output, - "Template rendering for `{}` did not produce the expected output (\"{}\"), instead got \"{}\"", - template, - expected_output, - output_as_str - ); + output_as_str, expected_output, + "Template rendering for `{}` did not produce the expected output (\"{}\"), instead got \"{}\"", + template, expected_output, output_as_str + ); } #[test] @@ -329,12 +323,9 @@ mod tests { ); let output_as_str = str::from_utf8(output.as_slice()).expect("Output was not UTF-8"); assert_eq!( - output_as_str, - expected_output, + output_as_str, expected_output, "Template rendering for content at '{}' did not produce the expected output (\"{}\"), instead got \"{}\"", - route, - expected_output, - output_as_str + route, expected_output, output_as_str ); } @@ -355,12 +346,9 @@ mod tests { ); let output_as_str = str::from_utf8(output.as_slice()).expect("Output was not UTF-8"); assert_eq!( - output_as_str, - expected_output, + output_as_str, expected_output, "Template rendering for content at '{}' did not produce the expected output (\"{}\"), instead got \"{}\"", - route, - expected_output, - output_as_str + route, expected_output, output_as_str ); } diff --git a/src/content/body.rs b/src/content/body.rs index d27eb56..2ec23ba 100644 --- a/src/content/body.rs +++ b/src/content/body.rs @@ -5,8 +5,8 @@ use super::StreamError; use crate::bug_message; use bytes::Bytes; -use futures::future::{Future, FutureExt, LocalBoxFuture}; use futures::Stream; +use futures::future::{Future, FutureExt, LocalBoxFuture}; use std::cmp; use std::fs::File; use std::io::ErrorKind::Interrupted; diff --git a/src/content/content_directory.rs b/src/content/content_directory.rs index b71a37b..0146219 100644 --- a/src/content/content_directory.rs +++ b/src/content/content_directory.rs @@ -122,7 +122,7 @@ impl ContentFile { return Err(ContentFileError(format!( "Non-unicode directory root (path is similar to '{}')", content_directory_root.display(), - ))) + ))); } }; diff --git a/src/content/content_engine.rs b/src/content/content_engine.rs index d751d0e..da2236c 100644 --- a/src/content/content_engine.rs +++ b/src/content/content_engine.rs @@ -7,8 +7,8 @@ use super::*; use crate::bug_message; use handlebars::{self, Handlebars}; use mime_guess::MimeGuess; -use std::collections::hash_map::Entry; use std::collections::HashMap; +use std::collections::hash_map::Entry; use std::path::Path; use std::sync::{Arc, RwLock}; use thiserror::Error; @@ -149,13 +149,13 @@ where return Err(ContentLoadingError::ContentFileNameError(format!( "Content file name '{}' has too many extensions.", entry.relative_path - ))) + ))); } [] => { return Err(ContentLoadingError::ContentFileNameError(format!( "Content file names must have extensions, but '{}' does not.", entry.relative_path - ))) + ))); } } } @@ -222,14 +222,12 @@ where // have the executable bit set. They are evaluated when rendered. [first_extension, Self::HANDLEBARS_FILE_EXTENSION] => { if content.is_executable { - return Err(ContentLoadingError::ContentFileNameError( - format!( - "The content file '{}' appears to be a handlebars file (because it ends in '.{}'), \ + return Err(ContentLoadingError::ContentFileNameError(format!( + "The content file '{}' appears to be a handlebars file (because it ends in '.{}'), \ but it is also executable. It must be one or the other.", - content.relative_path, - Self::HANDLEBARS_FILE_EXTENSION, - ), - )); + content.relative_path, + Self::HANDLEBARS_FILE_EXTENSION, + ))); } let mime = MimeGuess::from_ext(first_extension) @@ -480,12 +478,9 @@ mod tests { let actual_output = media_to_string(rendered); assert_eq!( - actual_output, - expected_output, + actual_output, expected_output, "Template rendering for `{}` did not produce the expected output (\"{}\"), instead got \"{}\"", - template, - expected_output, - actual_output, + template, expected_output, actual_output, ); } } @@ -539,12 +534,9 @@ mod tests { let actual_output = media_to_string(rendered); assert_eq!( - actual_output, - expected_output, + actual_output, expected_output, "Template rendering for `{}` did not produce the expected output (\"{}\"), instead got \"{}\"", - template, - expected_output, - actual_output, + template, expected_output, actual_output, ); } @@ -573,12 +565,9 @@ mod tests { let actual_output = media_to_string(rendered); assert_eq!( - actual_output, - expected_output, + actual_output, expected_output, "Rendering content at '{}' did not produce the expected output (\"{}\"), instead got \"{}\"", - route, - expected_output, - actual_output, + route, expected_output, actual_output, ); } @@ -639,12 +628,9 @@ mod tests { let actual_output = media_to_string(rendered); assert_eq!( - actual_output, - expected_output, + actual_output, expected_output, "Template rendering for `{}` did not produce the expected output (\"{}\"), instead got \"{}\"", - template, - expected_output, - actual_output, + template, expected_output, actual_output, ); } @@ -709,12 +695,9 @@ mod tests { let actual_output = media_to_string(rendered); assert_eq!( - actual_output, - expected_output, + actual_output, expected_output, "Template rendering for `{}` did not produce the expected output (\"{}\"), instead got \"{}\"", - template, - expected_output, - actual_output, + template, expected_output, actual_output, ); } @@ -750,12 +733,9 @@ mod tests { let actual_output = media_to_string(rendered); assert_eq!( - actual_output, - expected_output, + actual_output, expected_output, "Template rendering for `{}` did not produce the expected output (\"{}\"), instead got \"{}\"", - template, - expected_output, - actual_output, + template, expected_output, actual_output, ); } @@ -949,12 +929,9 @@ mod tests { let actual_output = media_to_string(rendered); assert_eq!( - actual_output, - expected_output, + actual_output, expected_output, "Rendering content at '{}' did not produce the expected output (\"{}\"), instead got \"{}\"", - route, - expected_output, - actual_output, + route, expected_output, actual_output, ); } @@ -980,12 +957,9 @@ mod tests { let actual_output = media_to_string(rendered); assert_eq!( - actual_output, - expected_output1, + actual_output, expected_output1, "Rendering content at '{}' did not produce the expected output (\"{}\"), instead got \"{}\"", - route1, - expected_output1, - actual_output, + route1, expected_output1, actual_output, ); let route2 = route("/subdirectory/pwd"); @@ -1003,12 +977,9 @@ mod tests { let actual_output = media_to_string(rendered); assert_eq!( - actual_output, - expected_output2, + actual_output, expected_output2, "Rendering content at '{}' did not produce the expected output (\"{}\"), instead got \"{}\"", - route2, - expected_output2, - actual_output, + route2, expected_output2, actual_output, ); } @@ -1099,12 +1070,9 @@ mod tests { let actual_output = media_to_string(rendered); assert_eq!( - actual_output, - expected_output, + actual_output, expected_output, "Rendering content at '{}' did not produce the expected output (\"{}\"), instead got \"{}\"", - route, - expected_output, - actual_output, + route, expected_output, actual_output, ); } diff --git a/src/content/content_index.rs b/src/content/content_index.rs index d369500..759c700 100644 --- a/src/content/content_index.rs +++ b/src/content/content_index.rs @@ -82,7 +82,7 @@ impl ContentIndexEntries { failed_route: route.clone(), message: format!( "There is already a resource at '{conficting_route}', but that needs to be a directory to accommodate the new route.", - ) + ), }); } }; diff --git a/src/content/content_item.rs b/src/content/content_item.rs index e6f72c5..6da9245 100644 --- a/src/content/content_item.rs +++ b/src/content/content_item.rs @@ -284,9 +284,9 @@ impl Executable { mod tests { use super::super::test_lib::*; use super::*; + use crate::ServerInfo; use crate::content::content_index::ContentIndexEntries; use crate::test_lib::*; - use crate::ServerInfo; use ::mime; use maplit::hashmap; use std::fs; diff --git a/src/content/content_registry.rs b/src/content/content_registry.rs index 5daa231..33299da 100644 --- a/src/content/content_registry.rs +++ b/src/content/content_registry.rs @@ -91,7 +91,7 @@ impl Render for ContentRepresentations { ))) } else { Ok(rendered) - } + }; } Err(error) => { log::warn!("Rendering failure: {error}"); diff --git a/src/content/handlebars_helpers/get.rs b/src/content/handlebars_helpers/get.rs index 6e212af..7498f11 100644 --- a/src/content/handlebars_helpers/get.rs +++ b/src/content/handlebars_helpers/get.rs @@ -122,12 +122,16 @@ where let modified_context_data_as_json = mem::take(Rc::make_mut(modified_context).data_mut()); match modified_context_data_as_json { - serde_json::Value::Object(modified_context_data_as_json_map) => modified_context_data_as_json_map, + serde_json::Value::Object(modified_context_data_as_json_map) => { + modified_context_data_as_json_map + } _ => { - return Err(handlebars::RenderError::from(handlebars::RenderErrorReason::Other(format!( - "The `get \"{route}\"` helper call failed because the pre-existing handlebars render context \ + return Err(handlebars::RenderError::from( + handlebars::RenderErrorReason::Other(format!( + "The `get \"{route}\"` helper call failed because the pre-existing handlebars render context \ was not an object (it was `{modified_context_data_as_json}`)." - )))) + )), + )); } } } diff --git a/src/http.rs b/src/http.rs index 0a20665..b4e6def 100644 --- a/src/http.rs +++ b/src/http.rs @@ -2,9 +2,9 @@ use crate::content::*; use crate::*; use actix_rt::System; use actix_web::error::QueryPayloadError; -use actix_web::http::header::{self, Header, HeaderMap}; use actix_web::http::HeaderValue; -use actix_web::{http, web, App, HttpRequest, HttpResponse, HttpServer}; +use actix_web::http::header::{self, Header, HeaderMap}; +use actix_web::{App, HttpRequest, HttpResponse, HttpServer, http, web}; use futures::TryStreamExt; use mime_guess::MimeGuess; use std::cmp::Ordering; @@ -1266,7 +1266,8 @@ mod tests { "Response status was not 404" ); assert_eq!( - response_body, "404 /not/a/real/path/so/this/should/404\nquery parameters:\nrequest headers:\n accept: text/plain", + response_body, + "404 /not/a/real/path/so/this/should/404\nquery parameters:\nrequest headers:\n accept: text/plain", "Response body was incorrect" ); } diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 08832d9..6a65fec 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -2,7 +2,7 @@ mod lib; use actix_web::client::Client as HttpClient; -use actix_web::http::{uri, Method, StatusCode}; +use actix_web::http::{Method, StatusCode, uri}; use lib::*; use operator::content::ContentDirectory; use operator::test_lib::*; diff --git a/tests/lib/mod.rs b/tests/lib/mod.rs index 3160c4d..a1b809a 100644 --- a/tests/lib/mod.rs +++ b/tests/lib/mod.rs @@ -3,12 +3,12 @@ use actix_web::error::PayloadError; use actix_web::http::StatusCode; use actix_web::test::unused_addr; use bytes::{Bytes, BytesMut}; -use futures::{future, Stream, TryStreamExt}; +use futures::{Stream, TryStreamExt, future}; use mime_guess::MimeGuess; use operator::content::{ContentDirectory, Route}; use operator::test_lib::*; -use std::collections::hash_map::DefaultHasher; use std::collections::HashMap; +use std::collections::hash_map::DefaultHasher; use std::env; use std::ffi::OsStr; use std::hash::Hasher; @@ -272,12 +272,18 @@ async fn render_multiple_ways_for_snapshots( if get_command_output.stdout.len() > 1024 { format!("{} bytes", get_command_output.stdout.len()) } else { - String::from(format!("{:?}", Bytes::from(get_command_output.stdout)).trim_end()) + String::from( + format!("{:?}", Bytes::from(get_command_output.stdout)) + .trim_end(), + ) }, if get_command_output.stderr.len() > 1024 { format!("{} bytes", get_command_output.stderr.len()) } else { - String::from(format!("{:?}", Bytes::from(get_command_output.stderr)).trim_end()) + String::from( + format!("{:?}", Bytes::from(get_command_output.stderr)) + .trim_end(), + ) }, http_response_status, if http_response_body.len() > 1024 {