40 lines
1.0 KiB
Rust
40 lines
1.0 KiB
Rust
use std::path::PathBuf;
|
|
use serde_derive::{Serialize, Deserialize};
|
|
use std::fs::read_to_string;
|
|
|
|
#[derive(Debug, Eq, PartialEq, Serialize, Deserialize)]
|
|
pub struct Image {
|
|
pub name: String,
|
|
/// colour indexes - todo convert to [u8; 64]?
|
|
pub pixels: Vec<u8>,
|
|
}
|
|
|
|
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 line in read_to_string(&path).unwrap().lines() {
|
|
for char in line.chars() {
|
|
pixels.push(char.to_string().parse().unwrap());
|
|
}
|
|
}
|
|
|
|
Self { name, pixels }
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
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);
|
|
}
|
|
}
|