create_thumbnail: switch to using AsRef<Path> where appropriate
- ID
92d1a04- date
2026-09-26 09:57:00+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
3f0b70b- message
create_thumbnail: switch to using AsRef<Path> where appropriate- changed files
5 files, 18 additions, 21 deletions
Changed files
src/create_parent_directory.rs (1212 → 1248)
diff --git a/src/create_parent_directory.rs b/src/create_parent_directory.rs
index 771cefe..e028978 100644
--- a/src/create_parent_directory.rs
+++ b/src/create_parent_directory.rs
@@ -1,6 +1,6 @@
use std::fs;
use std::io;
-use std::path::PathBuf;
+use std::path::Path;
/// Create the parent directory of a given path.
///
@@ -9,7 +9,9 @@ use std::path::PathBuf;
/// create_parent_directory("path/to/images/index.html")
/// ~> creates "path/to/images/"
///
-pub fn create_parent_directory(path: &PathBuf) -> io::Result<()> {
+pub fn create_parent_directory(path: impl AsRef<Path>) -> io::Result<()> {
+ let path = path.as_ref();
+
// Quoting from the Rust docs for PathBuf.parent() [1]:
//
// Returns None if the path terminates in a root or prefix,
src/create_thumbnail.rs (7687 → 7645)
diff --git a/src/create_thumbnail.rs b/src/create_thumbnail.rs
index e2eec24..ed107e0 100644
--- a/src/create_thumbnail.rs
+++ b/src/create_thumbnail.rs
@@ -1,4 +1,4 @@
-use std::path::PathBuf;
+use std::path::{Path, PathBuf};
use std::process::Command;
use std::str;
@@ -13,10 +13,11 @@ use crate::is_animated_gif::is_animated_gif;
/// Create a thumbnail for the image, and return the relative path of
/// the thumbnail within the collection folder.
pub fn create_thumbnail(
- path: &PathBuf,
+ path: impl AsRef<Path>,
out_dir: &PathBuf,
target: TargetDimension,
) -> Result<PathBuf, ThumbnailError> {
+ let path = path.as_ref();
let file_name = path.file_name().ok_or(ThumbnailError::MissingFileName)?;
let thumbnail_path = out_dir.join(file_name);
create_parent_directory(&thumbnail_path)?;
@@ -47,7 +48,7 @@ pub fn create_thumbnail(
/// installed, but I'm not sure how to simulate that.
///
fn create_animated_gif_thumbnail(
- gif_path: &PathBuf,
+ gif_path: &Path,
out_dir: &PathBuf,
width: u32,
height: u32,
@@ -112,7 +113,7 @@ fn ensure_even(x: u32) -> u32 {
/// This function assumes that the original image file definitely exists.
///
fn create_static_thumbnail(
- image_path: &PathBuf,
+ image_path: &Path,
out_dir: &PathBuf,
width: u32,
height: u32,
@@ -137,8 +138,6 @@ fn create_static_thumbnail(
#[cfg(test)]
mod test_create_thumbnail {
- use std::path::PathBuf;
-
use super::create_thumbnail;
use crate::dimensions::TargetDimension;
use crate::test_utils::{get_dimensions, test_dir};
@@ -155,9 +154,8 @@ mod test_create_thumbnail {
$(
#[test]
fn $name() {
- let path = PathBuf::from($img_path);
let out_dir = test_dir();
- let thumbnail_path = create_thumbnail(&path, &out_dir, $target).unwrap();
+ let thumbnail_path = create_thumbnail($img_path, &out_dir, $target).unwrap();
assert_eq!(thumbnail_path, out_dir.join($expected_filename));
assert!(thumbnail_path.exists());
src/dimensions.rs (5106 → 5047)
diff --git a/src/dimensions.rs b/src/dimensions.rs
index 7572c7d..352f87c 100644
--- a/src/dimensions.rs
+++ b/src/dimensions.rs
@@ -58,8 +58,6 @@ pub fn thumbnail_dimensions(
#[cfg(test)]
mod test_thumbnail_dimensions {
- use std::path::PathBuf;
-
use super::*;
macro_rules! target_fit_tests {
@@ -118,7 +116,7 @@ mod test_thumbnail_dimensions {
#[test]
fn errors_if_image_does_not_exist() {
- let p = PathBuf::from("src/tests/doesnotexist.png");
+ let p = "src/tests/doesnotexist.png";
let target = TargetDimension::MaxWidth(50);
@@ -128,7 +126,7 @@ mod test_thumbnail_dimensions {
#[test]
fn errors_if_cannot_read_image() {
- let p = PathBuf::from("README.md");
+ let p = "README.md";
let target = TargetDimension::MaxWidth(50);
src/is_animated_gif.rs (1615 → 1577)
diff --git a/src/is_animated_gif.rs b/src/is_animated_gif.rs
index 7636a26..7e8a543 100644
--- a/src/is_animated_gif.rs
+++ b/src/is_animated_gif.rs
@@ -1,12 +1,12 @@
use std::fs::File;
use std::io::{BufReader, Result};
-use std::path::PathBuf;
+use std::path::Path;
use image::codecs::gif::GifDecoder;
use image::AnimationDecoder;
/// Returns True if a file is an animated GIF, and False otherwise.
-pub fn is_animated_gif(path: &PathBuf) -> Result<bool> {
+pub fn is_animated_gif(path: impl AsRef<Path>) -> Result<bool> {
let file = File::open(path)?;
let reader = BufReader::new(file);
@@ -32,8 +32,7 @@ mod test_is_animated_gif {
$(
#[test]
fn $name() {
- let p = PathBuf::from($path);
- assert_eq!(is_animated_gif(&p).unwrap(), $is_animated);
+ assert_eq!(is_animated_gif($path).unwrap(), $is_animated);
}
)*
}
src/main.rs (9026 → 8978)
diff --git a/src/main.rs b/src/main.rs
index 729ee94..b32fa59 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -82,7 +82,7 @@ mod test_cli {
.stdout("/tmp/red.png")
.stderr("");
- assert_eq!(get_dimensions(&PathBuf::from("/tmp/red.png")), (50, 100));
+ assert_eq!(get_dimensions("/tmp/red.png"), (50, 100));
}
#[test]
@@ -95,7 +95,7 @@ mod test_cli {
.stdout("/tmp/noise.jpg")
.stderr("");
- assert_eq!(get_dimensions(&PathBuf::from("/tmp/noise.jpg")), (64, 128));
+ assert_eq!(get_dimensions("/tmp/noise.jpg"), (64, 128));
}
#[test]
@@ -113,7 +113,7 @@ mod test_cli {
.stdout("/tmp/noise.jpg")
.stderr("");
- assert_eq!(get_dimensions(&PathBuf::from("/tmp/noise.jpg")), (32, 64));
+ assert_eq!(get_dimensions("/tmp/noise.jpg"), (32, 64));
}
// Omitting `--out-dir` creates the thumbnail in CWD.