implement From for Image

This commit is contained in:
Max Bradbury 2020-04-12 11:26:59 +01:00
parent 2ec01ebfce
commit 163fddda5a
1 changed files with 17 additions and 16 deletions

View File

@ -424,7 +424,9 @@ fn example_game_default() -> Game {
}
}
fn image_from_string(string: String) -> Image {
impl From<String> for Image {
#[inline]
fn from(string: String) -> Image {
let string = string.replace("\n", "");
let pixels: Vec<&str> = string.split("").collect();
// the above seems to add an extra "" at the start and end of the vec, so strip them below
@ -434,13 +436,12 @@ fn image_from_string(string: String) -> Image {
}).collect();
Image { pixels }
}
}
#[test]
fn test_image_from_string() {
let output = image_from_string(
include_str!("../test/resources/image").to_string()
);
let output = Image::from(include_str!("../test/resources/image").to_string());
let expected = Image {
pixels: vec![
@ -532,7 +533,7 @@ fn tile_from_string(string: String) -> Tile {
let animation_frames = lines[1..].join("");
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
image_from_string(frame.to_string())
Image::from(frame.to_string())
}).collect();
Tile {id, name, wall, animation_frames}
@ -751,7 +752,7 @@ fn avatar_from_string(string: String) -> Avatar {
let animation_frames: String = lines.join("\n");
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
image_from_string(frame.to_string())
Image::from(frame.to_string())
}).collect();
Avatar { animation_frames, room, position }
@ -816,7 +817,7 @@ fn sprite_from_string(string: String) -> Sprite {
let animation_frames = lines[1..].join("");
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
image_from_string(frame.to_string())
Image::from(frame.to_string())
}).collect();
Sprite { id, name, animation_frames, dialogue, room, position }
@ -876,7 +877,7 @@ fn item_from_string(string: String) -> Item {
let animation_frames = lines[1..].join("");
let animation_frames: Vec<&str> = animation_frames.split("\n>\n").collect();
let animation_frames: Vec<Image> = animation_frames.iter().map(|&frame| {
image_from_string(frame.to_string())
Image::from(frame.to_string())
}).collect();
Item { id, name, animation_frames, dialogue }