use serde_derive::{Serialize, Deserialize}; #[derive(Debug, Eq, PartialEq, Serialize, Deserialize)] pub struct Image { pub name: String, /// colour indexes - todo convert to [u8; 64]? pub pixels: Vec, } impl Image { fn from(intermediate: IntermediateImage) -> Image { Image { name: intermediate.name.to_owned(), pixels: intermediate.pixels.split_whitespace().collect::().chars().map( |char|char as u8 ).collect() } } } /// for toml purposes #[derive(Debug, Serialize, Deserialize)] pub(crate) struct IntermediateImages { /// singular so each image is named "image" instead of "images" in toml image: Vec, } impl IntermediateImages { fn to_images(&self) -> Vec { self.image.iter().map(|intermediate| Image::from(intermediate.clone()) ).collect() } } #[derive(Clone, Debug, Serialize, Deserialize)] pub(crate) struct IntermediateImage { name: String, pixels: String, } impl IntermediateImage { // todo refactor fn from(image: Image) -> IntermediateImage { let mut string = "\n".to_string(); let sqrt = (image.pixels.len() as f64).sqrt() as usize; for line in image.pixels.chunks(sqrt) { for pixel in line { string.push_str(&format!("{}", *pixel)); } string.push('\n'); } IntermediateImage { name: image.name.to_owned(), /// todo wtf? I guess this crate doesn't handle multiline strings correctly pixels: format!("\"\"{}\"\"", string), } } } #[cfg(test)] mod test { // #[test] // fn test_image_from_toml() { // let str = include_str!("test-resources/basic/images.toml"); // let output: Image = toml::from_str(str).unwrap(); // let expected = crate::mock::image::avatar(); // assert_eq!(output, expected); // } }