From fbad036d8ebe2729533470226b108772a8d8e511 Mon Sep 17 00:00:00 2001 From: Ruby Nealon Date: Fri, 11 Oct 2024 17:40:55 +0900 Subject: [PATCH] replace glob with walkdir for OpenBSD support --- Cargo.lock | 1 - Cargo.toml | 1 - src/context.rs | 57 +++++++++++++++++++++----------------------------- 3 files changed, 24 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c1eeb72..7548aac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1482,7 +1482,6 @@ dependencies = [ "cached", "caches", "env_logger", - "glob", "handlebars", "httparse", "log", diff --git a/Cargo.toml b/Cargo.toml index 01eb035..9dc7478 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,6 @@ bytes = "1.6.0" cached = "0.53.1" caches = "0.2" env_logger = "0.10" # 0.11 requires 1.71 MSRV even as a dev-dep (due to manifest features) -glob = "0.3.1" handlebars = "6.1.0" httparse = "1.8.0" log = { version = "0.4.4" } diff --git a/src/context.rs b/src/context.rs index 30716c4..8417654 100644 --- a/src/context.rs +++ b/src/context.rs @@ -10,7 +10,6 @@ use crate::{ templates::{initialize_handlebars, DEFAULT_BLANK_PARTIAL_NAME}, }; use cached::stores::ExpiringSizedCache; -use glob::glob; use handlebars::Handlebars; use log::{debug, error}; use serde::Serialize; @@ -84,14 +83,10 @@ impl ServerContext { .into_iter() .filter_map(|e| e.ok()) { - let f_name = entry.file_name().to_string_lossy(); - - if f_name.ends_with(".hbs") { - let path_buf = entry.into_path(); - - let partial_name = path_buf - .to_str() - .unwrap() + let path_buf = entry.into_path(); + let path_str = path_buf.to_str().unwrap(); + if path_str.ends_with(".hbs") { + let partial_name = path_str .strip_prefix(&format!("{}/", self.config().partials_path())) .unwrap() .strip_suffix(".hbs") @@ -182,34 +177,30 @@ impl ServerContext { } pub fn get_data(&self) -> serde_json::Value { - let base_data_path = format!("{}/", self.config().data_path()); - let mut data = json!({}); - for entry in - glob(&format!("{}**/*.json", base_data_path)).expect("Failed to read data glob pattern") + for entry in WalkDir::new(self.config().data_path()) + .follow_links(false) + .into_iter() + .filter_map(|e| e.ok()) { - match entry { - Ok(path_buf) => { - let data_key = path_buf - .to_str() - .unwrap() - .strip_prefix(&base_data_path) - .unwrap() - .strip_suffix(".json") - .unwrap() - .to_string(); - match self.data_read(path_buf) { - Ok(value) => { - data.as_object_mut().unwrap().insert(data_key, value); - } - Err(err) => { - error!("ERROR reading data JSON file {}: {:?}", data_key, err) - } + let path_buf = entry.into_path(); + let path_str = path_buf.to_str().unwrap(); + if path_str.ends_with(".json") { + let data_key = path_str + .strip_prefix(&format!("{}/", self.config().data_path())) + .unwrap() + .strip_suffix(".json") + .unwrap() + .to_string(); + + match self.data_read(path_buf) { + Ok(value) => { + data.as_object_mut().unwrap().insert(data_key, value); + } + Err(err) => { + error!("ERROR reading data JSON file {}: {:?}", data_key, err) } - } - Err(err) => { - error!("ERROR loading JSON files by glob: {:?}", err) } } }