add Entity

This commit is contained in:
Max Bradbury 2021-05-19 11:07:49 +01:00
parent 0abc64946e
commit 6637d87d45
3 changed files with 45 additions and 0 deletions

42
src/entity.rs Normal file
View File

@ -0,0 +1,42 @@
use std::fs::read_to_string;
use std::path::PathBuf;
use serde_derive::{Serialize, Deserialize};
#[derive(Debug, Eq, PartialEq)]
pub struct Entity {
name: String,
image: String,
tags: Vec<String>,
}
impl Entity {
pub fn from_file(path: PathBuf) -> Self {
let name = path.file_stem().unwrap().to_str().unwrap().into();
let intermediate: IntermediateEntity = toml::from_str(
&read_to_string(path).unwrap()
).unwrap();
Self { name, image: intermediate.image, tags: intermediate.tags }
}
}
#[derive(Debug, Serialize, Deserialize)]
pub(crate) struct IntermediateEntity {
image: String,
tags: Vec<String>,
}
#[cfg(test)]
mod test {
use std::path::PathBuf;
use crate::entity::Entity;
#[test]
fn from_file() {
let path = PathBuf::from("src/test-resources/basic/entities/avatar.toml");
let output = Entity::from_file(path);
let expected = Entity { name: "avatar".into(), image: "avatar".into(), tags: vec![] };
assert_eq!(output, expected);
}
}

View File

@ -3,6 +3,7 @@ use std::path::PathBuf;
mod colour;
mod config;
mod entity;
mod image;
mod mock;
mod music;

View File

@ -0,0 +1,2 @@
image = "avatar"
tags = []