implement ToString for Image
This commit is contained in:
parent
57c9199bca
commit
4fd71f4a2c
30
src/main.rs
30
src/main.rs
|
@ -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());
|
||||
|
|
Loading…
Reference in New Issue