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;
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();
......
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));
}
......@@ -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,
......
......@@ -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,
},
};
......
......@@ -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);
......
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