From 27f2e68e03a72e8625b1e1e332bc5ff54d94071d Mon Sep 17 00:00:00 2001
From: David M <PieKing1215@users.noreply.github.com>
Date: Tue, 28 Dec 2021 20:02:00 -0500
Subject: [PATCH] Make layer ids optional and make tile layer name optional
 (like object layers)

Layer id defaults to 0 if missing, which is normally unused.
Tile layer name defaults to an empty String if missing, same as object layers.
---
 src/layers.rs  | 12 ++++++------
 src/objects.rs | 12 ++++++------
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/src/layers.rs b/src/layers.rs
index 19bb2ce..0834a83 100644
--- a/src/layers.rs
+++ b/src/layers.rs
@@ -67,19 +67,19 @@ impl Layer {
         layer_index: u32,
         infinite: bool,
     ) -> Result<Layer, TiledError> {
-        let ((o, v, ox, oy), (n, id)) = get_attrs!(
+        let ((o, v, ox, oy, n, id), ()) = get_attrs!(
             attrs,
             optionals: [
                 ("opacity", opacity, |v:String| v.parse().ok()),
                 ("visible", visible, |v:String| v.parse().ok().map(|x:i32| x == 1)),
                 ("offsetx", offset_x, |v:String| v.parse().ok()),
                 ("offsety", offset_y, |v:String| v.parse().ok()),
-            ],
-            required: [
                 ("name", name, |v| Some(v)),
                 ("id", id, |v:String| v.parse().ok()),
             ],
-            TiledError::MalformedAttributes("layer must have a name".to_string())
+            required: [],
+            // this error should never happen since there are no required attrs
+            TiledError::MalformedAttributes("layer parsing error".to_string())
         );
         let mut tiles: LayerData = LayerData::Finite(Default::default());
         let mut properties = HashMap::new();
@@ -99,7 +99,7 @@ impl Layer {
         });
 
         Ok(Layer {
-            name: n,
+            name: n.unwrap_or(String::new()),
             opacity: o.unwrap_or(1.0),
             visible: v.unwrap_or(true),
             offset_x: ox.unwrap_or(0.0),
@@ -107,7 +107,7 @@ impl Layer {
             tiles: tiles,
             properties: properties,
             layer_index,
-            id,
+            id: id.unwrap_or(0),
         })
     }
 }
diff --git a/src/objects.rs b/src/objects.rs
index 1329cc9..3b66e17 100644
--- a/src/objects.rs
+++ b/src/objects.rs
@@ -31,18 +31,18 @@ impl ObjectGroup {
         attrs: Vec<OwnedAttribute>,
         layer_index: Option<u32>,
     ) -> Result<ObjectGroup, TiledError> {
-        let ((o, v, c, n), id) = get_attrs!(
+        let ((o, v, c, n, id), ()) = get_attrs!(
             attrs,
             optionals: [
                 ("opacity", opacity, |v:String| v.parse().ok()),
                 ("visible", visible, |v:String| v.parse().ok().map(|x:i32| x == 1)),
                 ("color", colour, |v:String| v.parse().ok()),
-                ("name", name, |v:String| v.into()),
-            ],
-            required: [
+                ("name", name, |v:String| Some(v)),
                 ("id", id, |v:String| v.parse().ok()),
             ],
-            TiledError::MalformedAttributes("object groups must have a name".to_string())
+            required: [],
+            // this error should never happen since there are no required attrs
+            TiledError::MalformedAttributes("object group parsing error".to_string())
         );
         let mut objects = Vec::new();
         let mut properties = HashMap::new();
@@ -64,7 +64,7 @@ impl ObjectGroup {
             colour: c,
             layer_index,
             properties,
-            id,
+            id: id.unwrap_or(0),
         })
     }
 }
-- 
GitLab