From 37f0ee6971e8eb31b2ee6aecc6198bef723b70dd Mon Sep 17 00:00:00 2001 From: JToTheThree Date: Sat, 21 Dec 2024 14:30:30 -0600 Subject: [PATCH] reduce utf-8 checks on path iteration --- src/world.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/world.rs b/src/world.rs index aae01c3..2e21c3d 100644 --- a/src/world.rs +++ b/src/world.rs @@ -25,9 +25,11 @@ impl World { /// Utility function to test a single path against all defined patterns. /// Returns a parsed [`WorldMap`] on the first matched pattern or an error if no patterns match. pub fn match_path(&self, path: impl AsRef) -> Result { + let path_str = path.as_ref().to_str().expect("obtaining valid UTF-8 path"); + if let Some(patterns) = &self.patterns { for pattern in patterns { - match pattern.match_path(path.as_ref()) { + match pattern.match_path(path_str) { Ok(world_map) => return Ok(world_map), // We ignore matches here as the path may be matched by another pattern. Err(Error::NoMatchFound { .. }) => continue, @@ -99,7 +101,9 @@ impl WorldPattern { /// Utility function to test a path against this pattern. /// Returns a parsed [`WorldMap`] on the first matched pattern or an error if no patterns match. pub fn match_path(&self, path: impl AsRef) -> Result { - let captures = match self.regexp.captures(path.as_ref().to_str().unwrap()) { + let path_str = path.as_ref().to_str().expect("obtaining valid UTF-8 path"); + + let captures = match self.regexp.captures(path_str) { Some(captures) => captures, None => { return Err(Error::NoMatchFound { @@ -148,7 +152,7 @@ impl WorldPattern { ))?; Ok(WorldMap { - filename: path.as_ref().to_str().unwrap().to_string(), + filename: path_str.to_string(), x, y, width: None,