diff --git a/src/exit.rs b/src/exit.rs index d61da5d..5e0a09c 100644 --- a/src/exit.rs +++ b/src/exit.rs @@ -16,7 +16,7 @@ pub enum Transition { } impl Transition { - pub(crate) fn from_str(str: &str) -> Result { + pub fn from_str(str: &str) -> Result { match str { "fade_w" => Ok(Transition::FadeToWhite), "fade_b" => Ok(Transition::FadeToBlack), @@ -57,7 +57,7 @@ pub struct Exit { } impl Exit { - pub(crate) fn from_str(s: &str) -> Result { + pub fn from_str(s: &str) -> Result { let parts: Vec<&str> = s.split_whitespace().collect(); if parts.len() < 2 { diff --git a/src/image.rs b/src/image.rs index 7312c0c..bde4e6b 100644 --- a/src/image.rs +++ b/src/image.rs @@ -1,3 +1,5 @@ +use std::fmt; + #[derive(Clone, Debug, Eq, PartialEq)] pub struct Image { pub pixels: Vec, // 64 for SD, 256 for HD @@ -49,12 +51,9 @@ impl Image { self.pixels = pixels; } -} -impl From for Image { - fn from(string: String) -> Image { - let string = string.replace("NaN", "0"); - let string = string.trim(); + fn from_str(str: &str) -> Result { + let string = str.trim().replace("NaN", "0"); let lines: Vec<&str> = string.lines().collect(); let dimension = lines.len(); let mut pixels: Vec = Vec::new(); @@ -66,12 +65,12 @@ impl From for Image { } } - Image { pixels } + Ok(Image { pixels }) } } -impl ToString for Image { - fn to_string(&self) -> String { +impl fmt::Display for Image { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut string = String::new(); let sqrt = (self.pixels.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD @@ -84,26 +83,28 @@ impl ToString for Image { string.pop(); // remove trailing newline - string + write!(f, "{}", string) } } -pub(crate) fn animation_frames_from_string(string: String) -> Vec { - let frames: Vec<&str> = string.split('>').collect(); - - frames.iter().map(|&frame| Image::from(frame.to_string())).collect() +/// todo return Result<(Vec, Vec), crate::Error> +pub fn animation_frames_from_str(str: &str) -> Vec { + str + .split('>') + .collect::>() + .iter() + .map(|&frame| Image::from_str(frame).unwrap()) + .collect() } #[cfg(test)] mod test { - use crate::image::{Image, animation_frames_from_string}; + use crate::image::{Image, animation_frames_from_str}; use crate::mock; #[test] fn image_from_string() { - let output = Image::from( - include_str!("test-resources/image").to_string() - ); + let output = Image::from_str(include_str!("test-resources/image")).unwrap(); let expected = Image { pixels: vec![ @@ -130,8 +131,8 @@ mod test { #[test] fn test_animation_frames_from_string() { - let output = animation_frames_from_string( - include_str!("test-resources/animation_frames").to_string() + let output = animation_frames_from_str( + include_str!("test-resources/animation_frames") ); let expected = mock::image::animation_frames(); @@ -143,9 +144,7 @@ mod test { /// check that these extraneous pixels are stripped out #[test] fn image_out_of_bounds() { - let output = Image::from( - include_str!("test-resources/image-oob").to_string() - ); + let output = Image::from_str(include_str!("test-resources/image-oob")).unwrap(); let expected = Image { pixels: vec![ diff --git a/src/item.rs b/src/item.rs index 2998c44..4b083e3 100644 --- a/src/item.rs +++ b/src/item.rs @@ -1,5 +1,5 @@ use crate::{optional_data_line, AnimationFrames, Image}; -use crate::image::animation_frames_from_string; +use crate::image::animation_frames_from_str; #[derive(Clone, Debug, Eq, PartialEq)] pub struct Item { @@ -48,8 +48,8 @@ impl From for Item { } } - let animation_frames = animation_frames_from_string( - lines[1..].join("\n") + let animation_frames = animation_frames_from_str( + &lines[1..].join("\n") ); Item { diff --git a/src/sprite.rs b/src/sprite.rs index 05c647b..a14702a 100644 --- a/src/sprite.rs +++ b/src/sprite.rs @@ -1,5 +1,5 @@ use crate::{optional_data_line, AnimationFrames, Image, Position}; -use crate::image::animation_frames_from_string; +use crate::image::animation_frames_from_str; #[derive(Clone, Debug, Eq, PartialEq)] pub struct Sprite { @@ -100,8 +100,8 @@ impl Sprite { items.reverse(); - let animation_frames = animation_frames_from_string( - lines[1..].join("\n") + let animation_frames = animation_frames_from_str( + &lines[1..].join("\n") ); Ok(Sprite { diff --git a/src/tile.rs b/src/tile.rs index fcec446..f647975 100644 --- a/src/tile.rs +++ b/src/tile.rs @@ -1,5 +1,5 @@ use crate::{optional_data_line, AnimationFrames, Image}; -use crate::image::animation_frames_from_string; +use crate::image::animation_frames_from_str; #[derive(Clone, Debug, Eq)] pub struct Tile { @@ -103,8 +103,8 @@ impl From for Tile { } } - let animation_frames = animation_frames_from_string( - lines[1..].join("\n") + let animation_frames = animation_frames_from_str( + &lines[1..].join("\n") ); Tile {