From 411ea64fcb68935c9460635ee9d34448aa3ec31b Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 12 Nov 2024 01:14:48 +0100 Subject: [PATCH] walkdir --- Cargo.lock | 1 + Cargo.toml | 1 + src/utils/routes/blog.rs | 17 +++++++---------- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7381c93..b8d9515 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1095,6 +1095,7 @@ dependencies = [ "serde_yml", "toml", "urlencoding", + "walkdir", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 5ed8802..7246a9b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ mime_guess = "2.0" urlencoding = "2.1" regex = "1.10" cyborgtime = "2.1.1" +walkdir = "2.5" [lints.clippy] pedantic = "warn" diff --git a/src/utils/routes/blog.rs b/src/utils/routes/blog.rs index 78ac80f..b960f18 100644 --- a/src/utils/routes/blog.rs +++ b/src/utils/routes/blog.rs @@ -12,6 +12,7 @@ use chrono::{DateTime, Datelike, Local, NaiveDateTime, Utc}; use chrono_tz::Europe; use comrak::{parse_document, Arena}; use ramhorns::Content; +use walkdir::WalkDir; use crate::{ config::Config, @@ -59,16 +60,12 @@ impl Hash for Post { } pub fn get_posts(location: &str) -> Vec { - std::fs::read_dir(location) - .map_or_else( - |_| vec![], - |res| { - res.flatten() - .filter(|f| f.path().extension().map_or(false, |ext| ext == "md")) - .collect::>() - }, - ) - .iter() + WalkDir::new(location) + .into_iter() + .filter_map(Result::ok) + .filter(|entry| { + entry.file_type().is_file() && entry.path().extension().is_some_and(|s| s == "md") + }) .filter_map(|f| { let fname = f.file_name(); let filename = fname.to_string_lossy();