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,27 +458,29 @@ fn test_image_from_string() {
assert_eq!(output, expected)
}
fn image_to_string(image: Image) -> String {
let mut string = String::new();
impl ToString for Image {
#[inline]
fn to_string(&self) -> String {
let mut string = String::new();
let sqrt = (image.pixels.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD
for line in image.pixels.chunks(sqrt) {
for pixel in line {
string.push_str(&format!("{}", *pixel));
let sqrt = (self.pixels.len() as f64).sqrt() as usize; // 8 for SD, 16 for HD
for line in self.pixels.chunks(sqrt) {
for pixel in line {
string.push_str(&format!("{}", *pixel));
}
string.push('\n');
}
string.push('\n');
string.pop(); // remove trailing newline
string
}
string.pop(); // remove trailing newline
string
}
#[test]
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();
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;
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 {
string.push_str(&"\n>\n".to_string());