make some functions public; from_str functions; impl Display; error handling for Image
This commit is contained in:
parent
1c5315ddad
commit
0dcddb9d8e
|
@ -16,7 +16,7 @@ pub enum Transition {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
match str {
|
||||||
"fade_w" => Ok(Transition::FadeToWhite),
|
"fade_w" => Ok(Transition::FadeToWhite),
|
||||||
"fade_b" => Ok(Transition::FadeToBlack),
|
"fade_b" => Ok(Transition::FadeToBlack),
|
||||||
|
@ -57,7 +57,7 @@ pub struct Exit {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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();
|
let parts: Vec<&str> = s.split_whitespace().collect();
|
||||||
|
|
||||||
if parts.len() < 2 {
|
if parts.len() < 2 {
|
||||||
|
|
43
src/image.rs
43
src/image.rs
|
@ -1,3 +1,5 @@
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
#[derive(Clone, Debug, Eq, PartialEq)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub struct Image {
|
pub struct Image {
|
||||||
pub pixels: Vec<u8>, // 64 for SD, 256 for HD
|
pub pixels: Vec<u8>, // 64 for SD, 256 for HD
|
||||||
|
@ -49,12 +51,9 @@ impl Image {
|
||||||
|
|
||||||
self.pixels = pixels;
|
self.pixels = pixels;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
impl From<String> for Image {
|
fn from_str(str: &str) -> Result<Image, crate::Error> {
|
||||||
fn from(string: String) -> Image {
|
let string = str.trim().replace("NaN", "0");
|
||||||
let string = string.replace("NaN", "0");
|
|
||||||
let string = string.trim();
|
|
||||||
let lines: Vec<&str> = string.lines().collect();
|
let lines: Vec<&str> = string.lines().collect();
|
||||||
let dimension = lines.len();
|
let dimension = lines.len();
|
||||||
let mut pixels: Vec<u8> = Vec::new();
|
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 {
|
impl fmt::Display for Image {
|
||||||
fn to_string(&self) -> String {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
let mut string = String::new();
|
let mut string = String::new();
|
||||||
|
|
||||||
let sqrt = (self.pixels.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD
|
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.pop(); // remove trailing newline
|
||||||
|
|
||||||
string
|
write!(f, "{}", string)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn animation_frames_from_string(string: String) -> Vec<Image> {
|
/// todo return Result<(Vec<Image>, Vec<crate::Error>), crate::Error>
|
||||||
let frames: Vec<&str> = string.split('>').collect();
|
pub fn animation_frames_from_str(str: &str) -> Vec<Image> {
|
||||||
|
str
|
||||||
frames.iter().map(|&frame| Image::from(frame.to_string())).collect()
|
.split('>')
|
||||||
|
.collect::<Vec<&str>>()
|
||||||
|
.iter()
|
||||||
|
.map(|&frame| Image::from_str(frame).unwrap())
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use crate::image::{Image, animation_frames_from_string};
|
use crate::image::{Image, animation_frames_from_str};
|
||||||
use crate::mock;
|
use crate::mock;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn image_from_string() {
|
fn image_from_string() {
|
||||||
let output = Image::from(
|
let output = Image::from_str(include_str!("test-resources/image")).unwrap();
|
||||||
include_str!("test-resources/image").to_string()
|
|
||||||
);
|
|
||||||
|
|
||||||
let expected = Image {
|
let expected = Image {
|
||||||
pixels: vec![
|
pixels: vec![
|
||||||
|
@ -130,8 +131,8 @@ mod test {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_animation_frames_from_string() {
|
fn test_animation_frames_from_string() {
|
||||||
let output = animation_frames_from_string(
|
let output = animation_frames_from_str(
|
||||||
include_str!("test-resources/animation_frames").to_string()
|
include_str!("test-resources/animation_frames")
|
||||||
);
|
);
|
||||||
|
|
||||||
let expected = mock::image::animation_frames();
|
let expected = mock::image::animation_frames();
|
||||||
|
@ -143,9 +144,7 @@ mod test {
|
||||||
/// check that these extraneous pixels are stripped out
|
/// check that these extraneous pixels are stripped out
|
||||||
#[test]
|
#[test]
|
||||||
fn image_out_of_bounds() {
|
fn image_out_of_bounds() {
|
||||||
let output = Image::from(
|
let output = Image::from_str(include_str!("test-resources/image-oob")).unwrap();
|
||||||
include_str!("test-resources/image-oob").to_string()
|
|
||||||
);
|
|
||||||
|
|
||||||
let expected = Image {
|
let expected = Image {
|
||||||
pixels: vec![
|
pixels: vec![
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{optional_data_line, AnimationFrames, Image};
|
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)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub struct Item {
|
pub struct Item {
|
||||||
|
@ -48,8 +48,8 @@ impl From<String> for Item {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let animation_frames = animation_frames_from_string(
|
let animation_frames = animation_frames_from_str(
|
||||||
lines[1..].join("\n")
|
&lines[1..].join("\n")
|
||||||
);
|
);
|
||||||
|
|
||||||
Item {
|
Item {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{optional_data_line, AnimationFrames, Image, Position};
|
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)]
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
||||||
pub struct Sprite {
|
pub struct Sprite {
|
||||||
|
@ -100,8 +100,8 @@ impl Sprite {
|
||||||
|
|
||||||
items.reverse();
|
items.reverse();
|
||||||
|
|
||||||
let animation_frames = animation_frames_from_string(
|
let animation_frames = animation_frames_from_str(
|
||||||
lines[1..].join("\n")
|
&lines[1..].join("\n")
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(Sprite {
|
Ok(Sprite {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
use crate::{optional_data_line, AnimationFrames, Image};
|
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)]
|
#[derive(Clone, Debug, Eq)]
|
||||||
pub struct Tile {
|
pub struct Tile {
|
||||||
|
@ -103,8 +103,8 @@ impl From<String> for Tile {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let animation_frames = animation_frames_from_string(
|
let animation_frames = animation_frames_from_str(
|
||||||
lines[1..].join("\n")
|
&lines[1..].join("\n")
|
||||||
);
|
);
|
||||||
|
|
||||||
Tile {
|
Tile {
|
||||||
|
|
Loading…
Reference in New Issue