From a291833ea70cd9ec95a8a0ae0a81a75731a29afd Mon Sep 17 00:00:00 2001 From: Ticki Date: Sat, 6 Feb 2016 19:55:39 +0100 Subject: [PATCH] Make compatible with latest version of Rust --- Cargo.toml | 8 ++++---- src/lib.rs | 43 ++++++++++++++++++++++--------------------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 135f683e..677079b0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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/" @@ -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" diff --git a/src/lib.rs b/src/lib.rs index 640e78cf..c776e3a2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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)] @@ -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) { @@ -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())), _ => {} } } @@ -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) } @@ -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), } @@ -483,11 +485,10 @@ fn parse_data(parser: &mut EventReader, attrs: Vec, fn parse_base64(parser: &mut EventReader) -> Result, 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()); } @@ -522,8 +523,8 @@ fn decode_gzip(data: Vec) -> Result, TiledError> { fn decode_csv(parser: &mut EventReader) -> Result>, TiledError> { loop { - match parser.next() { - Characters(s) => { + match try!(parser.next().map_err(TiledError::XmlDecodingError)) { + XmlEvent::Characters(s) => { let mut rows: Vec> = Vec::new(); for row in s.split('\n') { if row.trim() == "" { @@ -533,7 +534,7 @@ fn decode_csv(parser: &mut EventReader) -> Result>, Til } return Ok(rows); } - EndElement {name, ..} => { + XmlEvent::EndElement {name, ..} => { if name.local_name == "data" { return Ok(Vec::new()); } @@ -565,13 +566,13 @@ fn convert_to_u32(all: &Vec, width: u32) -> Vec> { pub fn parse(reader: R) -> Result { 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())), _ => {} } }