diff --git a/src/world.rs b/src/world.rs index e7c8824..4be6d70 100644 --- a/src/world.rs +++ b/src/world.rs @@ -16,9 +16,11 @@ pub struct World { #[serde(skip_deserializing)] pub source: PathBuf, /// The [`WorldMap`]s defined by the world file. - pub maps: Option>, + #[serde(skip_serializing_if = "Vec::is_empty", default)] + pub maps: Vec, /// Optional regex pattern to load maps. - pub patterns: Option>, + #[serde(skip_serializing_if = "Vec::is_empty", default)] + pub patterns: Vec, } impl World { @@ -27,14 +29,12 @@ impl World { 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_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), } } diff --git a/tests/lib.rs b/tests/lib.rs index 4a6ff85..0779c85 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -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();