Skip to content
Snippets Groups Projects
Commit 485fb747 authored by alexdevteam's avatar alexdevteam
Browse files

Use `impl AsRef<Path>` where appropiate

parent 95a6aa2e
No related branches found
No related tags found
No related merge requests found
...@@ -28,7 +28,7 @@ use std::path::Path; ...@@ -28,7 +28,7 @@ use std::path::Path;
use tiled::parse; use tiled::parse;
fn main() { 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"); println!("Opened file");
let reader = BufReader::new(file); let reader = BufReader::new(file);
let map = parse(reader).unwrap(); let map = parse(reader).unwrap();
......
use std::fs::File;
use std::path::PathBuf;
use tiled::map::Map; use tiled::map::Map;
fn main() { fn main() {
let path = PathBuf::from("assets/tiled_base64_zlib.tmx"); let map = Map::parse_file("assets/tiled_base64_zlib.tmx").unwrap();
let file = File::open(&path).unwrap();
println!("Opened file");
let map = Map::parse_reader(file, Some(&path)).unwrap();
println!("{:?}", map); println!("{:?}", map);
println!("{:?}", map.tileset_by_gid(22)); println!("{:?}", map.tileset_by_gid(22));
} }
...@@ -18,12 +18,11 @@ use crate::{ ...@@ -18,12 +18,11 @@ use crate::{
util::{get_attrs, parse_tag}, 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)] #[derive(Debug, PartialEq, Clone)]
pub struct Map { pub struct Map {
/// The TMX format version this map was saved to. /// The TMX format version this map was saved to.
pub version: String, pub version: String,
/// The orientation of this map.
pub orientation: Orientation, pub orientation: Orientation,
/// Width of the map, in tiles. /// Width of the map, in tiles.
pub width: u32, pub width: u32,
...@@ -37,15 +36,12 @@ pub struct Map { ...@@ -37,15 +36,12 @@ pub struct Map {
pub tilesets: Vec<Tileset>, pub tilesets: Vec<Tileset>,
/// The tile layers present in this map. /// The tile layers present in this map.
pub layers: Vec<Layer>, pub layers: Vec<Layer>,
/// The image layers present in this map.
pub image_layers: Vec<ImageLayer>, pub image_layers: Vec<ImageLayer>,
/// The object groups present in this map.
pub object_groups: Vec<ObjectGroup>, pub object_groups: Vec<ObjectGroup>,
/// The custom properties of this map. /// The custom properties of this map.
pub properties: Properties, pub properties: Properties,
/// The background color of this map, if any. /// The background color of this map, if any.
pub background_color: Option<Color>, pub background_color: Option<Color>,
/// Whether this map is infinite or not.
pub infinite: bool, pub infinite: bool,
/// Where this map was loaded from. /// Where this map was loaded from.
/// If fully embedded (loaded with path = `None`), this will return `None`. /// If fully embedded (loaded with path = `None`), this will return `None`.
...@@ -82,10 +78,10 @@ impl Map { ...@@ -82,10 +78,10 @@ impl Map {
/// Parse a file hopefully containing a Tiled map and try to parse it. If the /// 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 /// file has an external tileset, the tileset file will be loaded using a path
/// relative to the map file's path. /// relative to the map file's path.
pub fn parse_file(path: &Path) -> Result<Self, TiledError> { pub fn parse_file(path: impl AsRef<Path>) -> Result<Self, TiledError> {
let file = File::open(path) let file = File::open(path.as_ref())
.map_err(|_| TiledError::Other(format!("Map file not found: {:?}", path)))?; .map_err(|_| TiledError::Other(format!("Map file not found: {:?}", path.as_ref())))?;
Self::parse_reader(file, Some(path)) Self::parse_reader(file, Some(path.as_ref()))
} }
fn parse_xml<R: Read>( fn parse_xml<R: Read>(
...@@ -173,6 +169,7 @@ impl Map { ...@@ -173,6 +169,7 @@ impl Map {
} }
} }
/// Represents the way tiles are laid out in a map.
#[derive(Debug, PartialEq, Eq, Copy, Clone)] #[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum Orientation { pub enum Orientation {
Orthogonal, Orthogonal,
......
...@@ -97,8 +97,11 @@ impl Tileset { ...@@ -97,8 +97,11 @@ impl Tileset {
let columns = match columns { let columns = match columns {
Some(col) => col, Some(col) => col,
None => match &image { None => match &image {
None => return Err(TiledError::MalformedAttributes( None => {
"No <image> and no <columns> in <tileset>".to_string())), return Err(TiledError::MalformedAttributes(
"No <image> and no <columns> in <tileset>".to_string(),
))
}
Some(image) => image.width as u32 / width, Some(image) => image.width as u32 / width,
}, },
}; };
...@@ -212,8 +215,11 @@ impl Tileset { ...@@ -212,8 +215,11 @@ impl Tileset {
let columns = match columns { let columns = match columns {
Some(col) => col, Some(col) => col,
None => match &image { None => match &image {
None => return Err(TiledError::MalformedAttributes( None => {
"No <image> and no <columns> in <tileset>".to_string())), return Err(TiledError::MalformedAttributes(
"No <image> and no <columns> in <tileset>".to_string(),
))
}
Some(image) => image.width as u32 / width, Some(image) => image.width as u32 / width,
}, },
}; };
......
...@@ -4,18 +4,18 @@ use tiled::{ ...@@ -4,18 +4,18 @@ use tiled::{
error::TiledError, layers::LayerData, map::Map, properties::PropertyValue, tileset::Tileset, 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(); let file = File::open(p).unwrap();
return Map::parse_reader(file, None); return Map::parse_reader(file, None);
} }
#[test] #[test]
fn test_gzip_and_zlib_encoded_and_raw_are_the_same() { 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 z = parse_map_without_source("assets/tiled_base64_zlib.tmx").unwrap();
let g = parse_map_without_source(&Path::new("assets/tiled_base64_gzip.tmx")).unwrap(); let g = parse_map_without_source("assets/tiled_base64_gzip.tmx").unwrap();
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 zstd = parse_map_without_source(&Path::new("assets/tiled_base64_zstandard.tmx")).unwrap(); let zstd = parse_map_without_source("assets/tiled_base64_zstandard.tmx").unwrap();
let c = parse_map_without_source(&Path::new("assets/tiled_csv.tmx")).unwrap(); let c = parse_map_without_source("assets/tiled_csv.tmx").unwrap();
assert_eq!(z, g); assert_eq!(z, g);
assert_eq!(z, r); assert_eq!(z, r);
assert_eq!(z, c); assert_eq!(z, c);
...@@ -37,8 +37,8 @@ fn test_gzip_and_zlib_encoded_and_raw_are_the_same() { ...@@ -37,8 +37,8 @@ fn test_gzip_and_zlib_encoded_and_raw_are_the_same() {
#[test] #[test]
fn test_external_tileset() { fn test_external_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 e = Map::parse_file(&Path::new("assets/tiled_base64_external.tmx")).unwrap(); let e = Map::parse_file("assets/tiled_base64_external.tmx").unwrap();
// Compare everything BUT source // Compare everything BUT source
assert_eq!(r.version, e.version); assert_eq!(r.version, e.version);
assert_eq!(r.orientation, e.orientation); assert_eq!(r.orientation, e.orientation);
...@@ -57,14 +57,14 @@ fn test_external_tileset() { ...@@ -57,14 +57,14 @@ fn test_external_tileset() {
#[test] #[test]
fn test_just_tileset() { 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(); let t = Tileset::parse(File::open(Path::new("assets/tilesheet.tsx")).unwrap(), 1).unwrap();
assert_eq!(r.tilesets[0], t); assert_eq!(r.tilesets[0], t);
} }
#[test] #[test]
fn test_infinite_tileset() { 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 { if let LayerData::Infinite(chunks) = &r.layers[0].tiles {
assert_eq!(chunks.len(), 4); assert_eq!(chunks.len(), 4);
...@@ -81,7 +81,7 @@ fn test_infinite_tileset() { ...@@ -81,7 +81,7 @@ fn test_infinite_tileset() {
#[test] #[test]
fn test_image_layers() { 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); assert_eq!(r.image_layers.len(), 2);
{ {
let first = &r.image_layers[0]; let first = &r.image_layers[0];
...@@ -107,7 +107,7 @@ fn test_image_layers() { ...@@ -107,7 +107,7 @@ fn test_image_layers() {
#[test] #[test]
fn test_tile_property() { 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)) = let prop_value: String = if let Some(&PropertyValue::StringValue(ref v)) =
r.tilesets[0].tiles[0].properties.get("a tile property") r.tilesets[0].tiles[0].properties.get("a tile property")
{ {
...@@ -120,7 +120,7 @@ fn test_tile_property() { ...@@ -120,7 +120,7 @@ fn test_tile_property() {
#[test] #[test]
fn test_object_group_property() { 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] let prop_value: bool = if let Some(&PropertyValue::BoolValue(ref v)) = r.object_groups[0]
.properties .properties
.get("an object group property") .get("an object group property")
...@@ -133,7 +133,7 @@ fn test_object_group_property() { ...@@ -133,7 +133,7 @@ fn test_object_group_property() {
} }
#[test] #[test]
fn test_tileset_property() { 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)) = let prop_value: String = if let Some(&PropertyValue::StringValue(ref v)) =
r.tilesets[0].properties.get("tileset property") r.tilesets[0].properties.get("tileset property")
{ {
...@@ -146,7 +146,7 @@ fn test_tileset_property() { ...@@ -146,7 +146,7 @@ fn test_tileset_property() {
#[test] #[test]
fn test_flipped_gid() { 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 { if let LayerData::Finite(tiles) = &r.layers[0].tiles {
let t1 = tiles[0][0]; let t1 = tiles[0][0];
...@@ -175,7 +175,7 @@ fn test_flipped_gid() { ...@@ -175,7 +175,7 @@ fn test_flipped_gid() {
#[test] #[test]
fn test_ldk_export() { 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 { if let LayerData::Finite(tiles) = &r.layers[0].tiles {
assert_eq!(tiles.len(), 8); assert_eq!(tiles.len(), 8);
assert_eq!(tiles[0].len(), 8); assert_eq!(tiles[0].len(), 8);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment