implement ToString for Image

This commit is contained in:
Max Bradbury 2020-04-12 10:24:21 +01:00
parent 57c9199bca
commit 4fd71f4a2c
1 changed files with 16 additions and 14 deletions

View File

@ -458,11 +458,13 @@ fn test_image_from_string() {
assert_eq!(output, expected) assert_eq!(output, expected)
} }
fn image_to_string(image: Image) -> String { impl ToString for Image {
#[inline]
fn to_string(&self) -> String {
let mut string = String::new(); let mut string = String::new();
let sqrt = (image.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
for line in image.pixels.chunks(sqrt) { for line in self.pixels.chunks(sqrt) {
for pixel in line { for pixel in line {
string.push_str(&format!("{}", *pixel)); string.push_str(&format!("{}", *pixel));
} }
@ -473,12 +475,12 @@ fn image_to_string(image: Image) -> String {
string string
} }
}
#[test] #[test]
fn test_image_to_string() { fn test_image_to_string() {
let output = image_to_string(example_image_chequers_1()); let output = example_image_chequers_1().to_string();
let expected = include_str!("../test/resources/image-chequers-1").to_string(); let expected = include_str!("../test/resources/image-chequers-1").to_string();
assert_eq!(output, expected); assert_eq!(output, expected);
} }
@ -487,7 +489,7 @@ fn animation_frames_to_string(animation_frames: Vec<Image>) -> String {
let last_frame = animation_frames.len() - 1; let last_frame = animation_frames.len() - 1;
for (i, frame) in animation_frames.into_iter().enumerate() { for (i, frame) in animation_frames.into_iter().enumerate() {
string.push_str(&image_to_string(frame)); string.push_str(&frame.to_string());
if i < last_frame { if i < last_frame {
string.push_str(&"\n>\n".to_string()); string.push_str(&"\n>\n".to_string());