Skip to content

Commit

Permalink
Merge pull request #16 from Ticki/master
Browse files Browse the repository at this point in the history
Make compatible with latest version of Rust
  • Loading branch information
mattyhall committed Feb 7, 2016
2 parents 532e44f + a291833 commit a80b71c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 25 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "tiled"
version = "0.1.4"
version = "0.1.5"
description = "A rust crate for loading in maps created by the Tiled editor"
repository = "https://github.com/mattyhall/rs-tiled.git"
# documentation = "http://rust-ci.org/mattyhall/rs-tiled/doc/tiled/"
Expand All @@ -18,6 +18,6 @@ name = "example"
path = "examples/main.rs"

[dependencies]
flate2 = "0.2"
rustc-serialize = "0.3"
xml-rs = "0.1.26"
flate2 = "0.2.13"
base64 = "0.1.1"
xml-rs = "0.3.0"
43 changes: 22 additions & 21 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
extern crate flate2;
extern crate xml;
extern crate rustc_serialize as serialize;
extern crate base64;

use std::str::FromStr;
use std::collections::HashMap;
use std::io::{BufReader, Read, Error};
use std::fmt;
use xml::reader::EventReader;
use xml::reader::events::XmlEvent::*;
use xml::reader::{EventReader, Error as XmlError};
use xml::reader::XmlEvent;
use xml::attribute::OwnedAttribute;
use serialize::base64::{FromBase64, FromBase64Error};
use base64::{u8de as decode_base64, Base64Error};
use flate2::read::{ZlibDecoder, GzDecoder};

#[derive(Debug)]
Expand Down Expand Up @@ -52,8 +52,8 @@ macro_rules! get_attrs {
macro_rules! parse_tag {
($parser:expr, $close_tag:expr, $($open_tag:expr => $open_method:expr),*) => {
loop {
match $parser.next() {
StartElement {name, attributes, ..} => {
match try!($parser.next().map_err(TiledError::XmlDecodingError)) {
XmlEvent::StartElement {name, attributes, ..} => {
if false {}
$(else if name.local_name == $open_tag {
match $open_method(attributes) {
Expand All @@ -62,12 +62,12 @@ macro_rules! parse_tag {
};
})*
}
EndElement {name, ..} => {
XmlEvent::EndElement {name, ..} => {
if name.local_name == $close_tag {
break;
}
}
EndDocument => return Err(TiledError::PrematureEnd("Document ended before we expected.".to_string())),
XmlEvent::EndDocument => return Err(TiledError::PrematureEnd("Document ended before we expected.".to_string())),
_ => {}
}
}
Expand Down Expand Up @@ -112,7 +112,8 @@ pub enum TiledError {
/// An error occured when decompressing using the
/// [flate2](https://github.com/alexcrichton/flate2-rs) crate.
DecompressingError(Error),
DecodingError(FromBase64Error),
Base64DecodingError(Base64Error),
XmlDecodingError(XmlError),
PrematureEnd(String),
Other(String)
}
Expand All @@ -122,7 +123,8 @@ impl fmt::Display for TiledError {
match *self {
TiledError::MalformedAttributes(ref s) => write!(fmt, "{}", s),
TiledError::DecompressingError(ref e) => write!(fmt, "{}", e),
TiledError::DecodingError(ref e) => write!(fmt, "{}", e),
TiledError::Base64DecodingError(ref e) => write!(fmt, "{}", e),
TiledError::XmlDecodingError(ref e) => write!(fmt, "{}", e),
TiledError::PrematureEnd(ref e) => write!(fmt, "{}", e),
TiledError::Other(ref s) => write!(fmt, "{}", s),
}
Expand Down Expand Up @@ -483,11 +485,10 @@ fn parse_data<R: Read>(parser: &mut EventReader<R>, attrs: Vec<OwnedAttribute>,

fn parse_base64<R: Read>(parser: &mut EventReader<R>) -> Result<Vec<u8>, TiledError> {
loop {
match parser.next() {
Characters(s) => return s.trim()
.from_base64()
.map_err(TiledError::DecodingError),
EndElement {name, ..} => {
match try!(parser.next().map_err(TiledError::XmlDecodingError)) {
XmlEvent::Characters(s) => return decode_base64(s.trim().as_bytes())
.map_err(TiledError::Base64DecodingError),
XmlEvent::EndElement {name, ..} => {
if name.local_name == "data" {
return Ok(Vec::new());
}
Expand Down Expand Up @@ -522,8 +523,8 @@ fn decode_gzip(data: Vec<u8>) -> Result<Vec<u8>, TiledError> {

fn decode_csv<R: Read>(parser: &mut EventReader<R>) -> Result<Vec<Vec<u32>>, TiledError> {
loop {
match parser.next() {
Characters(s) => {
match try!(parser.next().map_err(TiledError::XmlDecodingError)) {
XmlEvent::Characters(s) => {
let mut rows: Vec<Vec<u32>> = Vec::new();
for row in s.split('\n') {
if row.trim() == "" {
Expand All @@ -533,7 +534,7 @@ fn decode_csv<R: Read>(parser: &mut EventReader<R>) -> Result<Vec<Vec<u32>>, Til
}
return Ok(rows);
}
EndElement {name, ..} => {
XmlEvent::EndElement {name, ..} => {
if name.local_name == "data" {
return Ok(Vec::new());
}
Expand Down Expand Up @@ -565,13 +566,13 @@ fn convert_to_u32(all: &Vec<u8>, width: u32) -> Vec<Vec<u32>> {
pub fn parse<R: Read>(reader: R) -> Result<Map, TiledError> {
let mut parser = EventReader::new(reader);
loop {
match parser.next() {
StartElement {name, attributes, ..} => {
match try!(parser.next().map_err(TiledError::XmlDecodingError)) {
XmlEvent::StartElement {name, attributes, ..} => {
if name.local_name == "map" {
return Map::new(&mut parser, attributes);
}
}
EndDocument => return Err(TiledError::PrematureEnd("Document ended before map was parsed".to_string())),
XmlEvent::EndDocument => return Err(TiledError::PrematureEnd("Document ended before map was parsed".to_string())),
_ => {}
}
}
Expand Down

0 comments on commit a80b71c

Please sign in to comment.