From 7b941b5d994ee0887c377c582932e1a775d2199d Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Sun, 12 Apr 2020 11:29:13 +0100 Subject: [PATCH] implement From for Tile --- src/main.rs | 58 ++++++++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1eeb0ee..3babe5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -507,43 +507,43 @@ impl AnimationFrames for Vec { } } -fn tile_from_string(string: String) -> Tile { - let mut lines: Vec<&str> = string.lines().collect(); +impl From for Tile { + fn from(string: String) -> Tile { + let mut lines: Vec<&str> = string.lines().collect(); - let id = lines[0].replace("TIL ", ""); + let id = lines[0].replace("TIL ", ""); - let last_line = lines.pop().unwrap(); - let wall = match last_line == "WAL true" { - true => true, - false => { - lines.push(last_line); - false - } - }; + let last_line = lines.pop().unwrap(); + let wall = match last_line == "WAL true" { + true => true, + false => { + lines.push(last_line); + false + } + }; - let last_line = lines.pop().unwrap(); - let name = match last_line.starts_with("NAME") { - true => Some(last_line.replace("NAME ", "").to_string()), - false => { - lines.push(last_line); - None - } - }; + let last_line = lines.pop().unwrap(); + let name = match last_line.starts_with("NAME") { + true => Some(last_line.replace("NAME ", "").to_string()), + false => { + lines.push(last_line); + None + } + }; - let animation_frames = lines[1..].join(""); - let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect(); - let animation_frames: Vec = animation_frames.iter().map(|&frame| { - Image::from(frame.to_string()) - }).collect(); + let animation_frames = lines[1..].join(""); + let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect(); + let animation_frames: Vec = animation_frames.iter().map(|&frame| { + Image::from(frame.to_string()) + }).collect(); - Tile {id, name, wall, animation_frames} + Tile { id, name, wall, animation_frames } + } } #[test] fn test_tile_from_string() { - let output = tile_from_string( - include_str!("../test/resources/tile").to_string() - ); + let output = Tile::from(include_str!("../test/resources/tile").to_string()); let expected = Tile { id: "z".to_string(), @@ -1237,7 +1237,7 @@ fn game_from_string(string: String ) -> Game { } else if segment.starts_with("ROOM") { rooms.push(room_from_string(segment)); } else if segment.starts_with("TIL") { - tiles.push(tile_from_string(segment)); + tiles.push(Tile::from(segment)); } else if segment.starts_with("SPR A") { avatar = Some(avatar_from_string(segment)); } else if segment.starts_with("SPR") {