add Entity
This commit is contained in:
parent
0abc64946e
commit
6637d87d45
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -3,6 +3,7 @@ use std::path::PathBuf;
|
|||
|
||||
mod colour;
|
||||
mod config;
|
||||
mod entity;
|
||||
mod image;
|
||||
mod mock;
|
||||
mod music;
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
image = "avatar"
|
||||
tags = []
|
Loading…
Reference in New Issue