Skip to content
Snippets Groups Projects
Commit c9c64abb authored by Matthew Hall's avatar Matthew Hall
Browse files

Update to nightly. Again. Sigh

Something weird happens with slicing syntax. from_str changed to parse
parent e83d81c6
No related branches found
No related tags found
No related merge requests found
...@@ -10,6 +10,6 @@ fn main() { ...@@ -10,6 +10,6 @@ fn main() {
println!("Opened file"); println!("Opened file");
let reader = BufferedReader::new(file); let reader = BufferedReader::new(file);
let map = parse(reader).unwrap(); let map = parse(reader).unwrap();
println!("{}", map); println!("{:?}", map);
println!("{}", map.get_tileset_by_gid(22)); println!("{:?}", map.get_tileset_by_gid(22));
} }
...@@ -26,7 +26,7 @@ macro_rules! get_attrs { ...@@ -26,7 +26,7 @@ macro_rules! get_attrs {
$(let mut $oVar = None;)* $(let mut $oVar = None;)*
$(let mut $var = None;)* $(let mut $var = None;)*
for attr in $attrs.iter() { for attr in $attrs.iter() {
match attr.name.local_name[] { match attr.name.local_name.as_slice() {
$($oName => $oVar = $oMethod(attr.value.clone()),)* $($oName => $oVar = $oMethod(attr.value.clone()),)*
$($name => $var = $method(attr.value.clone()),)* $($name => $var = $method(attr.value.clone()),)*
_ => {} _ => {}
...@@ -50,7 +50,7 @@ macro_rules! parse_tag { ...@@ -50,7 +50,7 @@ macro_rules! parse_tag {
match $parser.next() { match $parser.next() {
StartElement {name, attributes, ..} => { StartElement {name, attributes, ..} => {
if false {} if false {}
$(else if name.local_name[] == $open_tag { $(else if name.local_name == $open_tag {
match $open_method(attributes) { match $open_method(attributes) {
Ok(()) => {}, Ok(()) => {},
Err(e) => return Err(e) Err(e) => return Err(e)
...@@ -58,7 +58,7 @@ macro_rules! parse_tag { ...@@ -58,7 +58,7 @@ macro_rules! parse_tag {
})* })*
} }
EndElement {name, ..} => { EndElement {name, ..} => {
if name.local_name[] == $close_tag { if name.local_name == $close_tag {
break; break;
} }
} }
...@@ -79,16 +79,16 @@ pub struct Colour { ...@@ -79,16 +79,16 @@ pub struct Colour {
impl FromStr for Colour { impl FromStr for Colour {
fn from_str(s: &str) -> Option<Colour> { fn from_str(s: &str) -> Option<Colour> {
let s = if s.starts_with("#") { let s = if s.starts_with("#") {
s[1..] &s[1..]
} else { } else {
s s
}; };
if s.len() != 6 { if s.len() != 6 {
return None; return None;
} }
let r = from_str_radix(s[0..2], 16); let r = from_str_radix(&s[0..2], 16);
let g = from_str_radix(s[2..4], 16); let g = from_str_radix(&s[2..4], 16);
let b = from_str_radix(s[4..6], 16); let b = from_str_radix(&s[4..6], 16);
if r.is_some() && g.is_some() && b.is_some() { if r.is_some() && g.is_some() && b.is_some() {
return Some(Colour {red: r.unwrap(), green: g.unwrap(), blue: b.unwrap()}) return Some(Colour {red: r.unwrap(), green: g.unwrap(), blue: b.unwrap()})
} }
...@@ -128,7 +128,7 @@ fn parse_properties<B: Buffer>(parser: &mut EventReader<B>) -> Result<Properties ...@@ -128,7 +128,7 @@ fn parse_properties<B: Buffer>(parser: &mut EventReader<B>) -> Result<Properties
Ok(p) Ok(p)
} }
/// All Tiled files will be parsed into this. Holds all the layers and tilesets /// All Tiled files will be parsed i32o this. Holds all the layers and tilesets
#[derive(Show)] #[derive(Show)]
pub struct Map { pub struct Map {
pub version: String, pub version: String,
...@@ -148,13 +148,13 @@ impl Map { ...@@ -148,13 +148,13 @@ impl Map {
fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>) -> Result<Map, TiledError> { fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>) -> Result<Map, TiledError> {
let (c, (v, o, w, h, tw, th)) = get_attrs!( let (c, (v, o, w, h, tw, th)) = get_attrs!(
attrs, attrs,
optionals: [("backgroundcolor", colour, |&:v:String| v[].parse())], optionals: [("backgroundcolor", colour, |&:v:String| v.parse())],
required: [("version", version, |&:v| Some(v)), required: [("version", version, |&:v| Some(v)),
("orientation", orientation, |&:v:String| v[].parse()), ("orientation", orientation, |&:v:String| v.parse()),
("width", width, |&:v:String| v[].parse()), ("width", width, |&:v:String| v.parse()),
("height", height, |&:v:String| v[].parse()), ("height", height, |&:v:String| v.parse()),
("tilewidth", tile_width, |&:v:String| v[].parse()), ("tilewidth", tile_width, |&:v:String| v.parse()),
("tileheight", tile_height, |&:v:String| v[].parse())], ("tileheight", tile_height, |&:v:String| v.parse())],
TiledError::MalformedAttributes("map must have a version, width and height with correct types".to_string())); TiledError::MalformedAttributes("map must have a version, width and height with correct types".to_string()));
let mut tilesets = Vec::new(); let mut tilesets = Vec::new();
...@@ -188,11 +188,11 @@ impl Map { ...@@ -188,11 +188,11 @@ impl Map {
/// This function will return the correct Tileset given a GID. /// This function will return the correct Tileset given a GID.
pub fn get_tileset_by_gid(&self, gid: u32) -> Option<&Tileset> { pub fn get_tileset_by_gid(&self, gid: u32) -> Option<&Tileset> {
let mut maximum_gid: int = -1; let mut maximum_gid: i32 = -1;
let mut maximum_ts = None; let mut maximum_ts = None;
for tileset in self.tilesets.iter() { for tileset in self.tilesets.iter() {
if tileset.first_gid as int > maximum_gid && tileset.first_gid < gid { if tileset.first_gid as i32 > maximum_gid && tileset.first_gid < gid {
maximum_gid = tileset.first_gid as int; maximum_gid = tileset.first_gid as i32;
maximum_ts = Some(tileset); maximum_ts = Some(tileset);
} }
} }
...@@ -237,12 +237,12 @@ impl Tileset { ...@@ -237,12 +237,12 @@ impl Tileset {
fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>) -> Result<Tileset, TiledError> { fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>) -> Result<Tileset, TiledError> {
let ((s, m), (g, n, w, h)) = get_attrs!( let ((s, m), (g, n, w, h)) = get_attrs!(
attrs, attrs,
optionals: [("spacing", spacing, |&:v:String| v[].parse()), optionals: [("spacing", spacing, |&:v:String| v.parse()),
("margin", margin, |&:v:String| v[].parse())], ("margin", margin, |&:v:String| v.parse())],
required: [("firstgid", first_gid, |&:v:String| v[].parse()), required: [("firstgid", first_gid, |&:v:String| v.parse()),
("name", name, |&:v| Some(v)), ("name", name, |&:v| Some(v)),
("tilewidth", width, |&:v:String| v[].parse()), ("tilewidth", width, |&:v:String| v.parse()),
("tileheight", height, |&:v:String| v[].parse())], ("tileheight", height, |&:v:String| v.parse())],
TiledError::MalformedAttributes("tileset must have a firstgid, name tile width and height with correct types".to_string())); TiledError::MalformedAttributes("tileset must have a firstgid, name tile width and height with correct types".to_string()));
let mut images = Vec::new(); let mut images = Vec::new();
...@@ -264,8 +264,8 @@ impl Tileset { ...@@ -264,8 +264,8 @@ impl Tileset {
pub struct Image { pub struct Image {
/// The filepath of the image /// The filepath of the image
pub source: String, pub source: String,
pub width: int, pub width: i32,
pub height: int, pub height: i32,
pub transparent_colour: Option<Colour>, pub transparent_colour: Option<Colour>,
} }
...@@ -273,10 +273,10 @@ impl Image { ...@@ -273,10 +273,10 @@ impl Image {
fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>) -> Result<Image, TiledError> { fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>) -> Result<Image, TiledError> {
let (c, (s, w, h)) = get_attrs!( let (c, (s, w, h)) = get_attrs!(
attrs, attrs,
optionals: [("trans", trans, |&:v:String| v[].parse())], optionals: [("trans", trans, |&:v:String| v.parse())],
required: [("source", source, |&:v| Some(v)), required: [("source", source, |&:v| Some(v)),
("width", width, |&:v:String| v[].parse()), ("width", width, |&:v:String| v.parse()),
("height", height, |&:v:String| v[].parse())], ("height", height, |&:v:String| v.parse())],
TiledError::MalformedAttributes("image must have a source, width and height with correct types".to_string())); TiledError::MalformedAttributes("image must have a source, width and height with correct types".to_string()));
parse_tag!(parser, "image", "" => |&:_| Ok(())); parse_tag!(parser, "image", "" => |&:_| Ok(()));
...@@ -299,8 +299,8 @@ impl Layer { ...@@ -299,8 +299,8 @@ impl Layer {
fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>, width: u32) -> Result<Layer, TiledError> { fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>, width: u32) -> Result<Layer, TiledError> {
let ((o, v), n) = get_attrs!( let ((o, v), n) = get_attrs!(
attrs, attrs,
optionals: [("opacity", opacity, |&:v:String| v[].parse()), optionals: [("opacity", opacity, |&:v:String| v.parse()),
("visible", visible, |&:v:String| v[].parse().map(|x:int| x == 1))], ("visible", visible, |&:v:String| v.parse().map(|x:i32| x == 1))],
required: [("name", name, |&:v| Some(v))], required: [("name", name, |&:v| Some(v))],
TiledError::MalformedAttributes("layer must have a name".to_string())); TiledError::MalformedAttributes("layer must have a name".to_string()));
let mut tiles = Vec::new(); let mut tiles = Vec::new();
...@@ -332,9 +332,9 @@ impl ObjectGroup { ...@@ -332,9 +332,9 @@ impl ObjectGroup {
fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>) -> Result<ObjectGroup, TiledError> { fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>) -> Result<ObjectGroup, TiledError> {
let ((o, v, c), n) = get_attrs!( let ((o, v, c), n) = get_attrs!(
attrs, attrs,
optionals: [("opacity", opacity, |&:v:String| v[].parse()), optionals: [("opacity", opacity, |&:v:String| v.parse()),
("visible", visible, |&:v:String| v[].parse().map(|x:int| x == 1)), ("visible", visible, |&:v:String| v.parse().map(|x:i32| x == 1)),
("color", colour, |&:v:String| v[].parse())], ("color", colour, |&:v:String| v.parse())],
required: [("name", name, |&:v| Some(v))], required: [("name", name, |&:v| Some(v))],
TiledError::MalformedAttributes("object groups must have a name".to_string())); TiledError::MalformedAttributes("object groups must have a name".to_string()));
let mut objects = Vec::new(); let mut objects = Vec::new();
...@@ -352,21 +352,21 @@ impl ObjectGroup { ...@@ -352,21 +352,21 @@ impl ObjectGroup {
#[derive(Show)] #[derive(Show)]
pub enum Object { pub enum Object {
Rect { x: int, y: int, width: u32, height: u32, visible: bool}, Rect { x: i32, y: i32, width: u32, height: u32, visible: bool},
Ellipse { x: int, y: int, width: u32, height: u32, visible: bool}, Ellipse { x: i32, y: i32, width: u32, height: u32, visible: bool},
Polyline { x: int, y: int, points: Vec<(int, int)>, visible: bool}, Polyline { x: i32, y: i32, points: Vec<(i32, i32)>, visible: bool},
Polygon { x: int, y: int, points: Vec<(int, int)>, visible: bool} Polygon { x: i32, y: i32, points: Vec<(i32, i32)>, visible: bool}
} }
impl Object { impl Object {
fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>) -> Result<Object, TiledError> { fn new<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>) -> Result<Object, TiledError> {
let ((w, h, v), (x, y)) = get_attrs!( let ((w, h, v), (x, y)) = get_attrs!(
attrs, attrs,
optionals: [("width", width, |&:v:String| v[].parse()), optionals: [("width", width, |&:v:String| v.parse()),
("height", height, |&:v:String| v[].parse()), ("height", height, |&:v:String| v.parse()),
("visible", visible, |&:v:String| v[].parse())], ("visible", visible, |&:v:String| v.parse())],
required: [("x", x, |&:v:String| v[].parse()), required: [("x", x, |&:v:String| v.parse()),
("y", y, |&:v:String| v[].parse())], ("y", y, |&:v:String| v.parse())],
TiledError::MalformedAttributes("objects must have an x and a y number".to_string())); TiledError::MalformedAttributes("objects must have an x and a y number".to_string()));
let mut obj = None; let mut obj = None;
let v = v.unwrap_or(true); let v = v.unwrap_or(true);
...@@ -400,7 +400,7 @@ impl Object { ...@@ -400,7 +400,7 @@ impl Object {
} }
} }
fn new_polyline(x: int, y: int, v: bool, attrs: Vec<OwnedAttribute>) -> Result<Object, TiledError> { fn new_polyline(x: i32, y: i32, v: bool, attrs: Vec<OwnedAttribute>) -> Result<Object, TiledError> {
let ((), s) = get_attrs!( let ((), s) = get_attrs!(
attrs, attrs,
optionals: [], optionals: [],
...@@ -410,7 +410,7 @@ impl Object { ...@@ -410,7 +410,7 @@ impl Object {
Ok(Object::Polyline {x: x, y: y, points: points, visible: v}) Ok(Object::Polyline {x: x, y: y, points: points, visible: v})
} }
fn new_polygon(x: int, y: int, v: bool, attrs: Vec<OwnedAttribute>) -> Result<Object, TiledError> { fn new_polygon(x: i32, y: i32, v: bool, attrs: Vec<OwnedAttribute>) -> Result<Object, TiledError> {
let ((), s) = get_attrs!( let ((), s) = get_attrs!(
attrs, attrs,
optionals: [], optionals: [],
...@@ -420,8 +420,8 @@ impl Object { ...@@ -420,8 +420,8 @@ impl Object {
Ok(Object::Polygon {x: x, y: y, points: points, visible: v}) Ok(Object::Polygon {x: x, y: y, points: points, visible: v})
} }
fn parse_points(s: String) -> Result<Vec<(int, int)>, TiledError> { fn parse_points(s: String) -> Result<Vec<(i32, i32)>, TiledError> {
let pairs = s[].split(' '); let pairs = s.split(' ');
let mut points = Vec::new(); let mut points = Vec::new();
for v in pairs.map(|&:p| p.splitn(1, ',')) { for v in pairs.map(|&:p| p.splitn(1, ',')) {
let v: Vec<&str> = v.clone().collect(); let v: Vec<&str> = v.clone().collect();
...@@ -430,7 +430,7 @@ impl Object { ...@@ -430,7 +430,7 @@ impl Object {
} }
let (x, y) = (v[0].parse(), v[1].parse()); let (x, y) = (v[0].parse(), v[1].parse());
if x.is_none() || y.is_none() { if x.is_none() || y.is_none() {
return Err(TiledError::MalformedAttributes("one of polyline's points does not have integer coordinates".to_string())); return Err(TiledError::MalformedAttributes("one of polyline's points does not have i32eger coordinates".to_string()));
} }
points.push((x.unwrap(), y.unwrap())); points.push((x.unwrap(), y.unwrap()));
} }
...@@ -445,15 +445,15 @@ fn parse_data<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute> ...@@ -445,15 +445,15 @@ fn parse_data<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>
required: [("encoding", encoding, |&:v| Some(v)), required: [("encoding", encoding, |&:v| Some(v)),
("compression", compression, |&:v| Some(v))], ("compression", compression, |&:v| Some(v))],
TiledError::MalformedAttributes("data must have an encoding and a compression".to_string())); TiledError::MalformedAttributes("data must have an encoding and a compression".to_string()));
if !(e[] == "base64" && c[] == "zlib") { if !(e == "base64" && c == "zlib") {
return Err(TiledError::Other("Only base64 and zlib allowed for the moment".to_string())); return Err(TiledError::Other("Only base64 and zlib allowed for the moment".to_string()));
} }
loop { loop {
match parser.next() { match parser.next() {
Characters(s) => { Characters(s) => {
match s[].trim().from_base64() { match s.trim().from_base64() {
Ok(v) => { Ok(v) => {
let mut zd = ZlibDecoder::new(BufReader::new(v[])); let mut zd = ZlibDecoder::new(BufReader::new(v.as_slice()));
let mut data = Vec::new(); let mut data = Vec::new();
let mut row = Vec::new(); let mut row = Vec::new();
loop { loop {
...@@ -462,7 +462,7 @@ fn parse_data<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute> ...@@ -462,7 +462,7 @@ fn parse_data<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>
Err(IoError{kind, ..}) if kind == EndOfFile => return Ok(data), Err(IoError{kind, ..}) if kind == EndOfFile => return Ok(data),
Err(e) => return Err(TiledError::DecompressingError(e)) Err(e) => return Err(TiledError::DecompressingError(e))
} }
if row.len() == width as uint { if row.len() as u32 == width {
data.push(row); data.push(row);
row = Vec::new(); row = Vec::new();
} }
...@@ -472,7 +472,7 @@ fn parse_data<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute> ...@@ -472,7 +472,7 @@ fn parse_data<B: Buffer>(parser: &mut EventReader<B>, attrs: Vec<OwnedAttribute>
} }
} }
EndElement {name, ..} => { EndElement {name, ..} => {
if name.local_name[] == "data" { if name.local_name == "data" {
return Ok(Vec::new()); return Ok(Vec::new());
} }
} }
...@@ -488,7 +488,7 @@ pub fn parse<B: Buffer>(reader: B) -> Result<Map, TiledError> { ...@@ -488,7 +488,7 @@ pub fn parse<B: Buffer>(reader: B) -> Result<Map, TiledError> {
loop { loop {
match parser.next() { match parser.next() {
StartElement {name, attributes, ..} => { StartElement {name, attributes, ..} => {
if name.local_name[] == "map" { if name.local_name == "map" {
return Map::new(&mut parser, attributes); return Map::new(&mut parser, attributes);
} }
} }
......
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