rework images as text

This commit is contained in:
2021-05-17 16:40:58 +01:00
parent 79b21e38ec
commit ff6552ce6b
4 changed files with 94 additions and 134 deletions

View File

@@ -1,4 +1,6 @@
use std::path::PathBuf;
use serde_derive::{Serialize, Deserialize};
use std::fs::read_to_string;
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Image {
@@ -7,66 +9,31 @@ pub struct Image {
pub pixels: Vec<u8>,
}
// impl Image {
// fn from(intermediate: IntermediateImage) -> Image {
// Image {
// name: intermediate.name.to_owned(),
// pixels: intermediate.pixels.split_whitespace().collect::<String>().chars().map(
// |char|char as u8
// ).collect()
// }
// }
// }
impl Image {
pub fn from_file(path: PathBuf) -> Self {
let name = path.file_stem().unwrap().to_str().unwrap().into();
let mut pixels: Vec<u8> = Vec::new();
/// 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<IntermediateImage>,
for line in read_to_string(&path).unwrap().lines() {
for char in line.chars() {
pixels.push(char.to_string().parse().unwrap());
}
}
Self { name, pixels }
}
}
// impl IntermediateImages {
// fn to_images(&self) -> Vec<Image> {
// 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);
// }
use std::path::PathBuf;
use crate::image::Image;
#[test]
fn image_from_text() {
let path = PathBuf::from("src/test-resources/basic/images/avatar.txt");
let output = Image::from_file(path);
let expected = crate::mock::image::avatar();
assert_eq!(output, expected);
}
}