Skip to content

Commit

Permalink
match_path and match_paths
Browse files Browse the repository at this point in the history
  • Loading branch information
JtotheThree committed Dec 19, 2024
1 parent 9eab95d commit b36eac9
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
6 changes: 3 additions & 3 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub enum Error {
/// Filename does not match any pattern in the world file.
NoMatchFound {
/// The filename that was not matched.
filename: String,
path: PathBuf,
},
/// A parameter is out of range or results in arithmetic underflow or overflow.
RangeError(String),
Expand Down Expand Up @@ -134,8 +134,8 @@ impl fmt::Display for Error {
#[cfg(feature = "world")]
Error::JsonDecodingError(e) => write!(fmt, "{}", e),
#[cfg(feature = "world")]
Error::NoMatchFound { filename } => {
write!(fmt, "No match found for filename: '{}'", filename)
Error::NoMatchFound { path } => {
write!(fmt, "No match found for path: '{}'", path.to_string_lossy())
}
Error::RangeError(e) => write!(fmt, "Range error: {}", e),
Error::PrematureEnd(e) => write!(fmt, "{}", e),
Expand Down
18 changes: 8 additions & 10 deletions src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,12 @@ pub struct World {
}

impl World {
/// Utility function to test a single filename against all defined patterns.
/// 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_filename(&self, filename: &str) -> Result<WorldMap, Error> {
// Tiled only tests tmx files that exist in the same directory as the world file.
// Supporting a proper path would misalign the crate with how tiled handles patterns.
pub fn match_path(&self, path: impl AsRef<Path>) -> Result<WorldMap, Error> {
if let Some(patterns) = &self.patterns {
for pattern in patterns {
let captures = match pattern.regexp.captures(filename) {
let captures = match pattern.regexp.captures(path.as_ref().to_str().unwrap()) {
Some(captures) => captures,
None => continue,
};
Expand Down Expand Up @@ -67,7 +65,7 @@ impl World {

// Returning the first matched pattern aligns with how Tiled handles patterns.
return Ok(WorldMap {
filename: filename.to_owned(),
filename: path.as_ref().to_str().unwrap().to_string(),
x,
y,
width: None,
Expand All @@ -77,16 +75,16 @@ impl World {
}

Err(Error::NoMatchFound {
filename: filename.to_string(),
path: path.as_ref().to_owned(),
})
}

/// Utility function to test a vec of filenames against all defined patterns.
/// Returns a vec of results with the parsed [`WorldMap`]s if it matches the pattern.
pub fn match_filenames(&self, filenames: &Vec<&str>) -> Vec<Result<WorldMap, Error>> {
filenames
pub fn match_paths<P: AsRef<Path>>(&self, paths: &[P]) -> Vec<Result<WorldMap, Error>> {
paths
.into_iter()
.map(|filename| self.match_filename(filename))
.map(|path| self.match_path(path))
.collect()
}
}
Expand Down
16 changes: 10 additions & 6 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,24 +99,28 @@ fn test_loading_world_pattern() {
let patterns = e.patterns.as_ref().unwrap();
assert_eq!(patterns.len(), 3);

let map1 = e.match_filename("map-x04-y04-plains.tmx").unwrap();
let map1 = e.match_path("map-x04-y04-plains.tmx").unwrap();

assert_eq!(map1.filename, "map-x04-y04-plains.tmx");
assert_eq!(map1.x, 2800);
assert_eq!(map1.y, 1680);

let map2 = e.match_filename("overworld-x02-y02.tmx").unwrap();
let map2 = e
.match_path(PathBuf::from("assets/overworld-x02-y02.tmx"))
.unwrap();

assert_eq!(map2.filename, "overworld-x02-y02.tmx");
assert_eq!(map2.filename, "assets/overworld-x02-y02.tmx");
// Test to determine if we correctly hit the second pattern
assert_eq!(map2.x, 5472);

let filenames = vec!["bad_map.tmx", "OVERFLOW-x099-y099.tmx"];
let paths = vec!["bad_map.tmx", "OVERFLOW-x099-y099.tmx"];

let errors = vec![
"No match found for filename: 'bad_map.tmx'",
"No match found for path: 'bad_map.tmx'",
"Range error: Capture x * multiplierX causes overflow",
];

let matches = e.match_filenames(&filenames);
let matches = e.match_paths(&paths);

for (index, result) in matches.iter().enumerate() {
assert_eq!(result.is_err(), true);
Expand Down

0 comments on commit b36eac9

Please sign in to comment.