use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct EntityId(String);
impl EntityId {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn into_string(self) -> String {
self.0
}
}
impl fmt::Display for EntityId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<&str> for EntityId {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl From<String> for EntityId {
fn from(s: String) -> Self {
Self(s)
}
}
impl AsRef<str> for EntityId {
fn as_ref(&self) -> &str {
&self.0
}
}
impl std::borrow::Borrow<str> for EntityId {
fn borrow(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Entity {
pub id: EntityId,
pub label: Option<String>,
pub entity_type: Option<String>,
#[serde(default)]
pub properties: std::collections::HashMap<String, serde_json::Value>,
}
impl Entity {
pub fn new(id: impl Into<EntityId>) -> Self {
Self {
id: id.into(),
label: None,
entity_type: None,
properties: std::collections::HashMap::new(),
}
}
pub fn with_label(mut self, label: impl Into<String>) -> Self {
self.label = Some(label.into());
self
}
pub fn with_type(mut self, entity_type: impl Into<String>) -> Self {
self.entity_type = Some(entity_type.into());
self
}
pub fn with_property(
mut self,
key: impl Into<String>,
value: impl Into<serde_json::Value>,
) -> Self {
self.properties.insert(key.into(), value.into());
self
}
}
impl fmt::Display for Entity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(ref label) = self.label {
write!(f, "{} ({})", label, self.id)
} else {
write!(f, "{}", self.id)
}
}
}