Skip to content

Commit

Permalink
formatting and better docs
Browse files Browse the repository at this point in the history
  • Loading branch information
JtotheThree committed Dec 18, 2024
1 parent 427dcb8 commit 9695ab2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
12 changes: 9 additions & 3 deletions src/loader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::path::Path;

use crate::{
DefaultResourceCache, FilesystemResourceReader, Map, ResourceCache, ResourceReader, Result, Tileset
DefaultResourceCache, FilesystemResourceReader, Map, ResourceCache, ResourceReader, Result,
Tileset,
};

#[cfg(feature = "world")]
Expand Down Expand Up @@ -184,9 +185,14 @@ impl<Cache: ResourceCache, Reader: ResourceReader> Loader<Cache, Reader> {
crate::parse::xml::parse_tileset(path.as_ref(), &mut self.reader, &mut self.cache)
}

/// Parses a file hopefully containing a Tiled world and tries to parse it. All external files
/// will be loaded relative to the path given.
#[cfg(feature = "world")]
/// Parses a file hopefully containing a Tiled world.
///
/// The returned [`World`] provides the deserialized data from the world file. It does not load
/// any maps or tilesets.
/// ## Note
/// The ['WorldPattern`] struct provides [`WorldPattern::capture_path`] and [`WorldPattern::capture_paths`]
/// as utility functions to test paths and return parsed [`WorldMap`]s.
pub fn load_world(&mut self, path: impl AsRef<Path>) -> Result<World> {
crate::world::parse_world(path.as_ref(), &mut self.reader)
}
Expand Down
12 changes: 9 additions & 3 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,20 +91,26 @@ fn test_loading_world() {
fn test_loading_world_pattern() {
let mut loader = Loader::new();

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

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

let patterns = e.patterns.unwrap();

assert_eq!(patterns.len(), 3);

let map1 = patterns[0].capture_path(&PathBuf::from("assets/world/map-x04-y04-plains.tmx")).unwrap();
let map1 = patterns[0]
.capture_path(&PathBuf::from("assets/world/map-x04-y04-plains.tmx"))
.unwrap();
assert_eq!(map1.filename, "assets/world/map-x04-y04-plains.tmx");
assert_eq!(map1.x, 2800);
assert_eq!(map1.y, 1680);

let map2 = patterns[1].capture_path(&PathBuf::from("overworld-x02-y02.tmx")).unwrap();
let map2 = patterns[1]
.capture_path(&PathBuf::from("overworld-x02-y02.tmx"))
.unwrap();
assert_eq!(map2.filename, "overworld-x02-y02.tmx");

let unmatched_map = patterns[0].capture_path(&PathBuf::from("bad_map.tmx"));
Expand Down

0 comments on commit 9695ab2

Please sign in to comment.