Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
rs-tiled
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Microhacks
rs-tiled
Commits
935057a9
Commit
935057a9
authored
5 years ago
by
TatriX
Browse files
Options
Downloads
Patches
Plain Diff
Refactor try to ?
parent
c671bd58
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
examples/main.rs
+0
-2
0 additions, 2 deletions
examples/main.rs
src/lib.rs
+25
-27
25 additions, 27 deletions
src/lib.rs
tests/lib.rs
+18
-9
18 additions, 9 deletions
tests/lib.rs
with
43 additions
and
38 deletions
examples/main.rs
+
0
−
2
View file @
935057a9
use
std
::
fs
::
File
;
use
std
::
path
::
Path
;
use
tiled
::
parse
;
...
...
This diff is collapsed.
Click to expand it.
src/lib.rs
+
25
−
27
View file @
935057a9
use
base64
;
use
std
::
collections
::
HashMap
;
use
std
::
fmt
;
use
std
::
fs
::
File
;
...
...
@@ -52,7 +50,7 @@ macro_rules! get_attrs {
macro_rules!
parse_tag
{
(
$parser:expr
,
$close_tag:expr
,
{
$
(
$open_tag:expr
=>
$open_method:expr
),
*
$
(,)
*
})
=>
{
loop
{
match
r
#
try!
(
$parser
.next
()
.map_err
(
TiledError
::
XmlDecodingError
)
)
{
match
$parser
.next
()
.map_err
(
TiledError
::
XmlDecodingError
)
?
{
XmlEvent
::
StartElement
{
name
,
attributes
,
..
}
=>
{
if
false
{}
$
(
else
if
name
.local_name
==
$open_tag
{
...
...
@@ -216,7 +214,7 @@ fn parse_properties<R: Read>(parser: &mut EventReader<R>) -> Result<Properties,
);
let
t
=
t
.unwrap_or
(
"string"
.into
());
p
.insert
(
k
,
r
#
try!
(
PropertyValue
::
new
(
t
,
v
)
)
);
p
.insert
(
k
,
PropertyValue
::
new
(
t
,
v
)
?
);
Ok
(())
},
});
...
...
@@ -270,25 +268,25 @@ impl Map {
let
mut
layer_index
=
0
;
parse_tag!
(
parser
,
"map"
,
{
"tileset"
=>
|
attrs
|
{
tilesets
.push
(
r
#
try!
(
Tileset
::
new
(
parser
,
attrs
,
map_path
)
)
);
tilesets
.push
(
Tileset
::
new
(
parser
,
attrs
,
map_path
)
?
);
Ok
(())
},
"layer"
=>
|
attrs
|
{
layers
.push
(
r
#
try!
(
Layer
::
new
(
parser
,
attrs
,
w
,
layer_index
)
)
);
layers
.push
(
Layer
::
new
(
parser
,
attrs
,
w
,
layer_index
)
?
);
layer_index
+=
1
;
Ok
(())
},
"imagelayer"
=>
|
attrs
|
{
image_layers
.push
(
r
#
try!
(
ImageLayer
::
new
(
parser
,
attrs
,
layer_index
)
)
);
image_layers
.push
(
ImageLayer
::
new
(
parser
,
attrs
,
layer_index
)
?
);
layer_index
+=
1
;
Ok
(())
},
"properties"
=>
|
_
|
{
properties
=
r
#
try!
(
parse_properties
(
parser
)
)
;
properties
=
parse_properties
(
parser
)
?
;
Ok
(())
},
"objectgroup"
=>
|
attrs
|
{
object_groups
.push
(
r
#
try!
(
ObjectGroup
::
new
(
parser
,
attrs
,
Some
(
layer_index
))
)
);
object_groups
.push
(
ObjectGroup
::
new
(
parser
,
attrs
,
Some
(
layer_index
))
?
);
layer_index
+=
1
;
Ok
(())
},
...
...
@@ -393,11 +391,11 @@ impl Tileset {
let
mut
tiles
=
Vec
::
new
();
parse_tag!
(
parser
,
"tileset"
,
{
"image"
=>
|
attrs
|
{
images
.push
(
r
#
try!
(
Image
::
new
(
parser
,
attrs
)
)
);
images
.push
(
Image
::
new
(
parser
,
attrs
)
?
);
Ok
(())
},
"tile"
=>
|
attrs
|
{
tiles
.push
(
r
#
try!
(
Tile
::
new
(
parser
,
attrs
)
)
);
tiles
.push
(
Tile
::
new
(
parser
,
attrs
)
?
);
Ok
(())
},
});
...
...
@@ -441,7 +439,7 @@ impl Tileset {
fn
new_external
<
R
:
Read
>
(
file
:
R
,
first_gid
:
u32
)
->
Result
<
Tileset
,
TiledError
>
{
let
mut
tileset_parser
=
EventReader
::
new
(
file
);
loop
{
match
r
#
try
!
(
tileset_parser
.next
()
.map_err
(
TiledError
::
XmlDecodingError
)
)
{
match
tileset_parser
.next
()
.map_err
(
TiledError
::
XmlDecodingError
)
?
{
XmlEvent
::
StartElement
{
name
,
attributes
,
..
}
=>
{
...
...
@@ -486,11 +484,11 @@ impl Tileset {
let
mut
tiles
=
Vec
::
new
();
parse_tag!
(
parser
,
"tileset"
,
{
"image"
=>
|
attrs
|
{
images
.push
(
r
#
try!
(
Image
::
new
(
parser
,
attrs
)
)
);
images
.push
(
Image
::
new
(
parser
,
attrs
)
?
);
Ok
(())
},
"tile"
=>
|
attrs
|
{
tiles
.push
(
r
#
try!
(
Tile
::
new
(
parser
,
attrs
)
)
);
tiles
.push
(
Tile
::
new
(
parser
,
attrs
)
?
);
Ok
(())
},
});
...
...
@@ -674,11 +672,11 @@ impl Layer {
let
mut
properties
=
HashMap
::
new
();
parse_tag!
(
parser
,
"layer"
,
{
"data"
=>
|
attrs
|
{
tiles
=
r
#
try!
(
parse_data
(
parser
,
attrs
,
width
)
)
;
tiles
=
parse_data
(
parser
,
attrs
,
width
)
?
;
Ok
(())
},
"properties"
=>
|
_
|
{
properties
=
r
#
try!
(
parse_properties
(
parser
)
)
;
properties
=
parse_properties
(
parser
)
?
;
Ok
(())
},
});
...
...
@@ -784,11 +782,11 @@ impl ObjectGroup {
let
mut
properties
=
HashMap
::
new
();
parse_tag!
(
parser
,
"objectgroup"
,
{
"object"
=>
|
attrs
|
{
objects
.push
(
r
#
try!
(
Object
::
new
(
parser
,
attrs
)
)
);
objects
.push
(
Object
::
new
(
parser
,
attrs
)
?
);
Ok
(())
},
"properties"
=>
|
_
|
{
properties
=
r
#
try!
(
parse_properties
(
parser
)
)
;
properties
=
parse_properties
(
parser
)
?
;
Ok
(())
},
});
...
...
@@ -869,15 +867,15 @@ impl Object {
Ok
(())
},
"polyline"
=>
|
attrs
|
{
shape
=
Some
(
r
#
try!
(
Object
::
new_polyline
(
attrs
)
)
);
shape
=
Some
(
Object
::
new_polyline
(
attrs
)
?
);
Ok
(())
},
"polygon"
=>
|
attrs
|
{
shape
=
Some
(
r
#
try!
(
Object
::
new_polygon
(
attrs
)
)
);
shape
=
Some
(
Object
::
new_polygon
(
attrs
)
?
);
Ok
(())
},
"properties"
=>
|
_
|
{
properties
=
r
#
try!
(
parse_properties
(
parser
)
)
;
properties
=
parse_properties
(
parser
)
?
;
Ok
(())
},
});
...
...
@@ -910,7 +908,7 @@ impl Object {
],
TiledError
::
MalformedAttributes
(
"A polyline must have points"
.to_string
())
);
let
points
=
r
#
try
!
(
Object
::
parse_points
(
s
)
)
;
let
points
=
Object
::
parse_points
(
s
)
?
;
Ok
(
ObjectShape
::
Polyline
{
points
:
points
})
}
...
...
@@ -923,7 +921,7 @@ impl Object {
],
TiledError
::
MalformedAttributes
(
"A polygon must have points"
.to_string
())
);
let
points
=
r
#
try
!
(
Object
::
parse_points
(
s
)
)
;
let
points
=
Object
::
parse_points
(
s
)
?
;
Ok
(
ObjectShape
::
Polygon
{
points
:
points
})
}
...
...
@@ -977,7 +975,7 @@ fn parse_animation<R: Read>(parser: &mut EventReader<R>) -> Result<Vec<Frame>, T
let
mut
animation
=
Vec
::
new
();
parse_tag!
(
parser
,
"animation"
,
{
"frame"
=>
|
attrs
|
{
animation
.push
(
r
#
try!
(
Frame
::
new
(
attrs
)
)
);
animation
.push
(
Frame
::
new
(
attrs
)
?
);
Ok
(())
},
});
...
...
@@ -1034,7 +1032,7 @@ fn parse_data<R: Read>(
fn
parse_base64
<
R
:
Read
>
(
parser
:
&
mut
EventReader
<
R
>
)
->
Result
<
Vec
<
u8
>
,
TiledError
>
{
loop
{
match
r
#
try
!
(
parser
.next
()
.map_err
(
TiledError
::
XmlDecodingError
)
)
{
match
parser
.next
()
.map_err
(
TiledError
::
XmlDecodingError
)
?
{
XmlEvent
::
Characters
(
s
)
=>
{
return
base64
::
decode
(
s
.trim
()
.as_bytes
())
.map_err
(
TiledError
::
Base64DecodingError
)
}
...
...
@@ -1073,7 +1071,7 @@ fn decode_gzip(data: Vec<u8>) -> Result<Vec<u8>, TiledError> {
fn
decode_csv
<
R
:
Read
>
(
parser
:
&
mut
EventReader
<
R
>
)
->
Result
<
Vec
<
Vec
<
LayerTile
>>
,
TiledError
>
{
loop
{
match
r
#
try
!
(
parser
.next
()
.map_err
(
TiledError
::
XmlDecodingError
)
)
{
match
parser
.next
()
.map_err
(
TiledError
::
XmlDecodingError
)
?
{
XmlEvent
::
Characters
(
s
)
=>
{
let
mut
rows
:
Vec
<
Vec
<
LayerTile
>>
=
Vec
::
new
();
for
row
in
s
.split
(
'\n'
)
{
...
...
@@ -1121,7 +1119,7 @@ fn convert_to_tile(all: &Vec<u8>, width: u32) -> Vec<Vec<LayerTile>> {
fn
parse_impl
<
R
:
Read
>
(
reader
:
R
,
map_path
:
Option
<&
Path
>
)
->
Result
<
Map
,
TiledError
>
{
let
mut
parser
=
EventReader
::
new
(
reader
);
loop
{
match
r
#
try
!
(
parser
.next
()
.map_err
(
TiledError
::
XmlDecodingError
)
)
{
match
parser
.next
()
.map_err
(
TiledError
::
XmlDecodingError
)
?
{
XmlEvent
::
StartElement
{
name
,
attributes
,
..
}
=>
{
...
...
This diff is collapsed.
Click to expand it.
tests/lib.rs
+
18
−
9
View file @
935057a9
use
std
::
path
::
Path
;
use
std
::
fs
::
File
;
use
tiled
::{
Map
,
TiledError
,
PropertyValue
,
parse
,
parse_file
,
parse_tileset
};
use
std
::
path
::
Path
;
use
tiled
::{
parse
,
parse_file
,
parse_tileset
,
Map
,
PropertyValue
,
TiledError
};
fn
read_from_file
(
p
:
&
Path
)
->
Result
<
Map
,
TiledError
>
{
let
file
=
File
::
open
(
p
)
.unwrap
();
...
...
@@ -45,23 +43,31 @@ fn test_image_layers() {
{
let
first
=
&
r
.image_layers
[
0
];
assert_eq!
(
first
.name
,
"Image Layer 1"
);
assert!
(
first
.image
.is_none
(),
"{}'s image should be None"
,
first
.name
);
assert!
(
first
.image
.is_none
(),
"{}'s image should be None"
,
first
.name
);
}
{
let
second
=
&
r
.image_layers
[
1
];
assert_eq!
(
second
.name
,
"Image Layer 2"
);
let
image
=
second
.image
.as_ref
()
.expect
(
&
format!
(
"{}'s image shouldn't be None"
,
second
.name
));
let
image
=
second
.image
.as_ref
()
.expect
(
&
format!
(
"{}'s image shouldn't be None"
,
second
.name
));
assert_eq!
(
image
.source
,
"tilesheet.png"
);
assert_eq!
(
image
.width
,
448
);
assert_eq!
(
image
.height
,
192
);
}
}
#[test]
fn
test_tile_property
()
{
let
r
=
read_from_file
(
&
Path
::
new
(
"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"
)
{
let
prop_value
:
String
=
if
let
Some
(
&
PropertyValue
::
StringValue
(
ref
v
))
=
r
.tilesets
[
0
]
.tiles
[
0
]
.properties
.get
(
"a tile property"
)
{
v
.clone
()
}
else
{
String
::
new
()
...
...
@@ -72,7 +78,10 @@ fn test_tile_property() {
#[test]
fn
test_object_group_property
()
{
let
r
=
read_from_file
(
&
Path
::
new
(
"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"
)
{
let
prop_value
:
bool
=
if
let
Some
(
&
PropertyValue
::
BoolValue
(
ref
v
))
=
r
.object_groups
[
0
]
.properties
.get
(
"an object group property"
)
{
*
v
}
else
{
false
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment