From 6637d87d4548fc322edd3318dc6a85340b093171 Mon Sep 17 00:00:00 2001 From: Max Bradbury Date: Wed, 19 May 2021 11:07:49 +0100 Subject: [PATCH] add Entity --- src/entity.rs | 42 +++++++++++++++++++ src/lib.rs | 1 + src/test-resources/basic/entities/avatar.toml | 2 + 3 files changed, 45 insertions(+) create mode 100644 src/entity.rs create mode 100644 src/test-resources/basic/entities/avatar.toml diff --git a/src/entity.rs b/src/entity.rs new file mode 100644 index 0000000..a9a837e --- /dev/null +++ b/src/entity.rs @@ -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, +} + +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, +} + +#[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); + } +} diff --git a/src/lib.rs b/src/lib.rs index c58fdff..759c148 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ use std::path::PathBuf; mod colour; mod config; +mod entity; mod image; mod mock; mod music; diff --git a/src/test-resources/basic/entities/avatar.toml b/src/test-resources/basic/entities/avatar.toml new file mode 100644 index 0000000..56f537a --- /dev/null +++ b/src/test-resources/basic/entities/avatar.toml @@ -0,0 +1,2 @@ +image = "avatar" +tags = []