use std::{fs::File, io::{Error, Write}}; pub trait Output { fn write(filename: &str, pixels: &Vec, width: usize, height: usize) -> Result; } pub struct P3 {} impl Output for P3 { fn write(filename: &str, pixels: &Vec, width: usize, height: usize) -> Result { let mut file = File::create(filename)?; file.write(format!("P3\n{} {}\n255\n", width, height).as_bytes())?; let lines: Result, Error> = pixels .chunks(3) .map(|chunk| format!("{} {} {}\n", chunk[0], chunk[1], chunk[2])) .map(|line| file.write(line.as_bytes())) .collect(); lines.map(|_v| file) } }