1use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6pub type EntityId = u64;
8
9#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
11pub enum EntityType {
12 Task,
14 Action,
16 Step,
18 Object,
20 Location,
22 Appliance,
24 Custom(String),
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct Entity {
31 pub id: EntityId,
33 pub entity_type: EntityType,
35 pub name: String,
37 pub properties: HashMap<String, String>,
39}
40
41impl Entity {
42 pub fn new(id: EntityId, entity_type: EntityType, name: impl Into<String>) -> Self {
44 Self {
45 id,
46 entity_type,
47 name: name.into(),
48 properties: HashMap::new(),
49 }
50 }
51
52 pub fn with_property(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
54 self.properties.insert(key.into(), value.into());
55 self
56 }
57
58 pub fn property(&self, key: &str) -> Option<&str> {
60 self.properties.get(key).map(|s| s.as_str())
61 }
62}
63
64#[cfg(test)]
65mod tests {
66 use super::*;
67
68 #[test]
69 fn entity_creation() {
70 let e = Entity::new(1, EntityType::Task, "heat_then_place")
71 .with_property("n_steps", "7")
72 .with_property("difficulty", "hard");
73
74 assert_eq!(e.id, 1);
75 assert_eq!(e.entity_type, EntityType::Task);
76 assert_eq!(e.name, "heat_then_place");
77 assert_eq!(e.property("n_steps"), Some("7"));
78 assert_eq!(e.property("missing"), None);
79 }
80}