use serde::{Deserialize, Serialize};
use std::collections::HashMap;
pub type EntityId = u64;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum EntityType {
Task,
Action,
Step,
Object,
Location,
Appliance,
Custom(String),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Entity {
pub id: EntityId,
pub entity_type: EntityType,
pub name: String,
pub properties: HashMap<String, String>,
}
impl Entity {
pub fn new(id: EntityId, entity_type: EntityType, name: impl Into<String>) -> Self {
Self {
id,
entity_type,
name: name.into(),
properties: HashMap::new(),
}
}
pub fn with_property(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.properties.insert(key.into(), value.into());
self
}
pub fn property(&self, key: &str) -> Option<&str> {
self.properties.get(key).map(|s| s.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn entity_creation() {
let e = Entity::new(1, EntityType::Task, "heat_then_place")
.with_property("n_steps", "7")
.with_property("difficulty", "hard");
assert_eq!(e.id, 1);
assert_eq!(e.entity_type, EntityType::Task);
assert_eq!(e.name, "heat_then_place");
assert_eq!(e.property("n_steps"), Some("7"));
assert_eq!(e.property("missing"), None);
}
}