peachy/src/entity.rs

43 lines
1.1 KiB
Rust
Raw Normal View History

2021-05-19 11:07:49 +01:00
use std::fs::read_to_string;
use std::path::PathBuf;
use serde_derive::{Serialize, Deserialize};
#[derive(Debug, Eq, PartialEq)]
pub struct Entity {
pub name: String,
pub image: String,
pub tags: Vec<String>,
2021-05-19 11:07:49 +01:00
}
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);
}
}