make some functions public; from_str functions; impl Display; error handling for Image

This commit is contained in:
Max Bradbury 2020-10-18 15:14:12 +01:00
parent 1c5315ddad
commit 0dcddb9d8e
5 changed files with 32 additions and 33 deletions

View File

@ -16,7 +16,7 @@ pub enum Transition {
}
impl Transition {
pub(crate) fn from_str(str: &str) -> Result<Transition, crate::Error> {
pub fn from_str(str: &str) -> Result<Transition, crate::Error> {
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<Self, crate::Error> {
pub fn from_str(s: &str) -> Result<Self, crate::Error> {
let parts: Vec<&str> = s.split_whitespace().collect();
if parts.len() < 2 {

View File

@ -1,3 +1,5 @@
use std::fmt;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Image {
pub pixels: Vec<u8>, // 64 for SD, 256 for HD
@ -49,12 +51,9 @@ impl Image {
self.pixels = pixels;
}
}
impl From<String> for Image {
fn from(string: String) -> Image {
let string = string.replace("NaN", "0");
let string = string.trim();
fn from_str(str: &str) -> Result<Image, crate::Error> {
let string = str.trim().replace("NaN", "0");
let lines: Vec<&str> = string.lines().collect();
let dimension = lines.len();
let mut pixels: Vec<u8> = Vec::new();
@ -66,12 +65,12 @@ impl From<String> 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<Image> {
let frames: Vec<&str> = string.split('>').collect();
frames.iter().map(|&frame| Image::from(frame.to_string())).collect()
/// todo return Result<(Vec<Image>, Vec<crate::Error>), crate::Error>
pub fn animation_frames_from_str(str: &str) -> Vec<Image> {
str
.split('>')
.collect::<Vec<&str>>()
.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![

View File

@ -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<String> 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 {

View File

@ -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 {

View File

@ -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<String> 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 {