From 48e5ef02eb7ce371060baffee16c266782290445 Mon Sep 17 00:00:00 2001 From: bjorn3 <17426603+bjorn3@users.noreply.github.com> Date: Mon, 8 Jan 2024 19:48:06 +0100 Subject: [PATCH] Compare actual filenames rather than their lossy conversions This is both faster and more correct. --- src/recipe_find.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/recipe_find.rs b/src/recipe_find.rs index 286e62a89..1cb551696 100644 --- a/src/recipe_find.rs +++ b/src/recipe_find.rs @@ -1,3 +1,4 @@ +use std::ffi::OsStr; use std::fs::{self}; use std::path::{Path, PathBuf}; @@ -8,11 +9,11 @@ pub fn recipe_find(recipe: &str, dir: &Path) -> Result<Option<PathBuf>, String> } for entry in fs::read_dir(dir).map_err(|e| e.to_string())? { let entry = entry.map_err(|e| e.to_string())?; - if entry.file_name().to_string_lossy() == "recipe.sh" - || entry.file_name().to_string_lossy() == "recipe.toml" + if entry.file_name() == OsStr::new("recipe.sh") + || entry.file_name() == OsStr::new("recipe.toml") { // println!("recipe is {:?}", dir.file_name()); - if dir.file_name().unwrap().to_string_lossy() != recipe { + if dir.file_name().unwrap() != OsStr::new(recipe) { return Ok(None); } else { return Ok(Some(dir.to_path_buf())); @@ -51,8 +52,8 @@ pub fn list_recipes(dir: &Path) -> Result<Vec<String>, String> { } for entry in fs::read_dir(dir).map_err(|e| e.to_string())? { let entry = entry.map_err(|e| e.to_string())?; - if entry.file_name().to_string_lossy() == "recipe.sh" - || entry.file_name().to_string_lossy() == "recipe.toml" + if entry.file_name() == OsStr::new("recipe.sh") + || entry.file_name() == OsStr::new("recipe.toml") { recipes.push(dir.file_name().ok_or(format!("could not unwrap the filename for {:?}", dir))?.to_string_lossy().to_string()); return Ok(recipes); -- GitLab