diff --git a/README.md b/README.md index f92ec5fa6010bc2286657c3e9223734bb520cedb..e9972d6b55eafb3b538cbb07d50176d2f49a27fe 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ use std::path::Path; use tiled::parse; fn main() { - let file = File::open(&Path::new("assets/tiled_base64_zlib.tmx")).unwrap(); + let file = File::open("assets/tiled_base64_zlib.tmx").unwrap(); println!("Opened file"); let reader = BufReader::new(file); let map = parse(reader).unwrap(); diff --git a/examples/main.rs b/examples/main.rs index c683639689f56f202e4e62dfa59d6489ee98f4dc..afdcb6d209980f26edeec11c2e21b7537d822815 100644 --- a/examples/main.rs +++ b/examples/main.rs @@ -1,13 +1,7 @@ -use std::fs::File; -use std::path::PathBuf; - use tiled::map::Map; fn main() { - let path = PathBuf::from("assets/tiled_base64_zlib.tmx"); - let file = File::open(&path).unwrap(); - println!("Opened file"); - let map = Map::parse_reader(file, Some(&path)).unwrap(); + let map = Map::parse_file("assets/tiled_base64_zlib.tmx").unwrap(); println!("{:?}", map); println!("{:?}", map.tileset_by_gid(22)); } diff --git a/src/map.rs b/src/map.rs index 7fb0342c1807e94a139f85b4a82283f956bf9333..ec9d3c9c5714e668c5c5778b59ff877888bb09b4 100644 --- a/src/map.rs +++ b/src/map.rs @@ -18,12 +18,11 @@ use crate::{ util::{get_attrs, parse_tag}, }; -/// All Tiled files will be parsed into this. Holds all the layers and tilesets +/// All Tiled map files will be parsed into this. Holds all the layers and tilesets. #[derive(Debug, PartialEq, Clone)] pub struct Map { /// The TMX format version this map was saved to. pub version: String, - /// The orientation of this map. pub orientation: Orientation, /// Width of the map, in tiles. pub width: u32, @@ -37,15 +36,12 @@ pub struct Map { pub tilesets: Vec<Tileset>, /// The tile layers present in this map. pub layers: Vec<Layer>, - /// The image layers present in this map. pub image_layers: Vec<ImageLayer>, - /// The object groups present in this map. pub object_groups: Vec<ObjectGroup>, /// The custom properties of this map. pub properties: Properties, /// The background color of this map, if any. pub background_color: Option<Color>, - /// Whether this map is infinite or not. pub infinite: bool, /// Where this map was loaded from. /// If fully embedded (loaded with path = `None`), this will return `None`. @@ -82,10 +78,10 @@ impl Map { /// Parse a file hopefully containing a Tiled map and try to parse it. If the /// file has an external tileset, the tileset file will be loaded using a path /// relative to the map file's path. - pub fn parse_file(path: &Path) -> Result<Self, TiledError> { - let file = File::open(path) - .map_err(|_| TiledError::Other(format!("Map file not found: {:?}", path)))?; - Self::parse_reader(file, Some(path)) + pub fn parse_file(path: impl AsRef<Path>) -> Result<Self, TiledError> { + let file = File::open(path.as_ref()) + .map_err(|_| TiledError::Other(format!("Map file not found: {:?}", path.as_ref())))?; + Self::parse_reader(file, Some(path.as_ref())) } fn parse_xml<R: Read>( @@ -173,6 +169,7 @@ impl Map { } } +/// Represents the way tiles are laid out in a map. #[derive(Debug, PartialEq, Eq, Copy, Clone)] pub enum Orientation { Orthogonal, diff --git a/src/tileset.rs b/src/tileset.rs index 41fb009e8cb4f8a9fefb202a64ecb281596a88ea..8a0dfd4012b75b5ae5d192e344e8059cd0d8d654 100644 --- a/src/tileset.rs +++ b/src/tileset.rs @@ -97,8 +97,11 @@ impl Tileset { let columns = match columns { Some(col) => col, None => match &image { - None => return Err(TiledError::MalformedAttributes( - "No <image> and no <columns> in <tileset>".to_string())), + None => { + return Err(TiledError::MalformedAttributes( + "No <image> and no <columns> in <tileset>".to_string(), + )) + } Some(image) => image.width as u32 / width, }, }; @@ -212,8 +215,11 @@ impl Tileset { let columns = match columns { Some(col) => col, None => match &image { - None => return Err(TiledError::MalformedAttributes( - "No <image> and no <columns> in <tileset>".to_string())), + None => { + return Err(TiledError::MalformedAttributes( + "No <image> and no <columns> in <tileset>".to_string(), + )) + } Some(image) => image.width as u32 / width, }, }; diff --git a/tests/lib.rs b/tests/lib.rs index 6ac07b41dc56954226cb1a7dab739808cd4f7911..fa29fc5e225e33a2f3ab4c676bd3e99e85ebf9f1 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -4,18 +4,18 @@ use tiled::{ error::TiledError, layers::LayerData, map::Map, properties::PropertyValue, tileset::Tileset, }; -fn parse_map_without_source(p: &Path) -> Result<Map, TiledError> { +fn parse_map_without_source(p: impl AsRef<Path>) -> Result<Map, TiledError> { let file = File::open(p).unwrap(); return Map::parse_reader(file, None); } #[test] fn test_gzip_and_zlib_encoded_and_raw_are_the_same() { - let z = parse_map_without_source(&Path::new("assets/tiled_base64_zlib.tmx")).unwrap(); - let g = parse_map_without_source(&Path::new("assets/tiled_base64_gzip.tmx")).unwrap(); - let r = parse_map_without_source(&Path::new("assets/tiled_base64.tmx")).unwrap(); - let zstd = parse_map_without_source(&Path::new("assets/tiled_base64_zstandard.tmx")).unwrap(); - let c = parse_map_without_source(&Path::new("assets/tiled_csv.tmx")).unwrap(); + let z = parse_map_without_source("assets/tiled_base64_zlib.tmx").unwrap(); + let g = parse_map_without_source("assets/tiled_base64_gzip.tmx").unwrap(); + let r = parse_map_without_source("assets/tiled_base64.tmx").unwrap(); + let zstd = parse_map_without_source("assets/tiled_base64_zstandard.tmx").unwrap(); + let c = parse_map_without_source("assets/tiled_csv.tmx").unwrap(); assert_eq!(z, g); assert_eq!(z, r); assert_eq!(z, c); @@ -37,8 +37,8 @@ fn test_gzip_and_zlib_encoded_and_raw_are_the_same() { #[test] fn test_external_tileset() { - let r = parse_map_without_source(&Path::new("assets/tiled_base64.tmx")).unwrap(); - let e = Map::parse_file(&Path::new("assets/tiled_base64_external.tmx")).unwrap(); + let r = parse_map_without_source("assets/tiled_base64.tmx").unwrap(); + let e = Map::parse_file("assets/tiled_base64_external.tmx").unwrap(); // Compare everything BUT source assert_eq!(r.version, e.version); assert_eq!(r.orientation, e.orientation); @@ -57,14 +57,14 @@ fn test_external_tileset() { #[test] fn test_just_tileset() { - let r = parse_map_without_source(&Path::new("assets/tiled_base64.tmx")).unwrap(); + let r = parse_map_without_source("assets/tiled_base64.tmx").unwrap(); let t = Tileset::parse(File::open(Path::new("assets/tilesheet.tsx")).unwrap(), 1).unwrap(); assert_eq!(r.tilesets[0], t); } #[test] fn test_infinite_tileset() { - let r = Map::parse_file(&Path::new("assets/tiled_base64_zlib_infinite.tmx")).unwrap(); + let r = Map::parse_file("assets/tiled_base64_zlib_infinite.tmx").unwrap(); if let LayerData::Infinite(chunks) = &r.layers[0].tiles { assert_eq!(chunks.len(), 4); @@ -81,7 +81,7 @@ fn test_infinite_tileset() { #[test] fn test_image_layers() { - let r = parse_map_without_source(&Path::new("assets/tiled_image_layers.tmx")).unwrap(); + let r = parse_map_without_source("assets/tiled_image_layers.tmx").unwrap(); assert_eq!(r.image_layers.len(), 2); { let first = &r.image_layers[0]; @@ -107,7 +107,7 @@ fn test_image_layers() { #[test] fn test_tile_property() { - let r = parse_map_without_source(&Path::new("assets/tiled_base64.tmx")).unwrap(); + let r = parse_map_without_source("assets/tiled_base64.tmx").unwrap(); let prop_value: String = if let Some(&PropertyValue::StringValue(ref v)) = r.tilesets[0].tiles[0].properties.get("a tile property") { @@ -120,7 +120,7 @@ fn test_tile_property() { #[test] fn test_object_group_property() { - let r = parse_map_without_source(&Path::new("assets/tiled_object_groups.tmx")).unwrap(); + let r = parse_map_without_source("assets/tiled_object_groups.tmx").unwrap(); let prop_value: bool = if let Some(&PropertyValue::BoolValue(ref v)) = r.object_groups[0] .properties .get("an object group property") @@ -133,7 +133,7 @@ fn test_object_group_property() { } #[test] fn test_tileset_property() { - let r = parse_map_without_source(&Path::new("assets/tiled_base64.tmx")).unwrap(); + let r = parse_map_without_source("assets/tiled_base64.tmx").unwrap(); let prop_value: String = if let Some(&PropertyValue::StringValue(ref v)) = r.tilesets[0].properties.get("tileset property") { @@ -146,7 +146,7 @@ fn test_tileset_property() { #[test] fn test_flipped_gid() { - let r = Map::parse_file(&Path::new("assets/tiled_flipped.tmx")).unwrap(); + let r = Map::parse_file("assets/tiled_flipped.tmx").unwrap(); if let LayerData::Finite(tiles) = &r.layers[0].tiles { let t1 = tiles[0][0]; @@ -175,7 +175,7 @@ fn test_flipped_gid() { #[test] fn test_ldk_export() { - let r = Map::parse_file(&Path::new("assets/ldk_tiled_export.tmx")).unwrap(); + let r = Map::parse_file("assets/ldk_tiled_export.tmx").unwrap(); if let LayerData::Finite(tiles) = &r.layers[0].tiles { assert_eq!(tiles.len(), 8); assert_eq!(tiles[0].len(), 8);