Skip to content

Commit

Permalink
empty vecs instead of option
Browse files Browse the repository at this point in the history
  • Loading branch information
JtotheThree committed Dec 27, 2024
1 parent 584b893 commit 7930290
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 23 deletions.
20 changes: 10 additions & 10 deletions src/world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ pub struct World {
#[serde(skip_deserializing)]
pub source: PathBuf,
/// The [`WorldMap`]s defined by the world file.
pub maps: Option<Vec<WorldMap>>,
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub maps: Vec<WorldMap>,
/// Optional regex pattern to load maps.
pub patterns: Option<Vec<WorldPattern>>,
#[serde(skip_serializing_if = "Vec::is_empty", default)]
pub patterns: Vec<WorldPattern>,
}

impl World {
Expand All @@ -27,14 +29,12 @@ impl World {
pub fn match_path(&self, path: impl AsRef<Path>) -> Result<WorldMap, Error> {
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_impl(path_str) {
Ok(world_map) => return Ok(world_map),
// We ignore match errors here as the path may be matched by another pattern.
Err(Error::NoMatchFound { .. }) => continue,
Err(err) => return Err(err),
}
for pattern in self.patterns.iter() {
match pattern.match_path_impl(path_str) {
Ok(world_map) => return Ok(world_map),
// We ignore match errors here as the path may be matched by another pattern.
Err(Error::NoMatchFound { .. }) => continue,
Err(err) => return Err(err),
}
}

Expand Down
23 changes: 10 additions & 13 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,34 +70,31 @@ fn test_external_tileset() {

#[cfg(feature = "world")]
#[test]
fn test_loading_world() {
fn test_world() {
let mut loader = Loader::new();

let e = loader.load_world("assets/world/world_basic.world").unwrap();

let maps = e.maps.unwrap();

assert_eq!(maps[0].filename, "map01.tmx");
assert_eq!(maps[1].x, 960);
assert_eq!(maps[1].y, 0);
assert_eq!(maps[1].width, Some(960));
assert_eq!(maps[1].height, Some(640));
assert_eq!(maps.len(), 2);
assert_eq!(e.maps[0].filename, "map01.tmx");
assert_eq!(e.maps[1].x, 960);
assert_eq!(e.maps[1].y, 0);
assert_eq!(e.maps[1].width, Some(960));
assert_eq!(e.maps[1].height, Some(640));
assert_eq!(e.maps.len(), 2);
}

#[cfg(feature = "world")]
#[test]
fn test_loading_world_pattern() {
fn test_world_pattern() {
let mut loader = Loader::new();

let e = loader
.load_world("assets/world/world_pattern.world")
.unwrap();

assert_eq!(e.maps.is_none(), true);
assert_eq!(e.maps.len(), 0);

let patterns = e.patterns.as_ref().unwrap();
assert_eq!(patterns.len(), 3);
assert_eq!(e.patterns.len(), 3);

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

Expand Down

0 comments on commit 7930290

Please sign in to comment.