use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct EntityId(pub u64);
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct EntityName(pub String);
impl EntityName {
pub fn new(name: impl Into<String>) -> Self {
Self(name.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for EntityName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl std::fmt::Display for EntityId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
#[serde(untagged)]
pub enum EntityRef {
Id(EntityId),
Name(String),
}
impl EntityRef {
pub fn name(name: impl Into<String>) -> Self {
Self::Name(name.into())
}
pub fn id(id: u64) -> Self {
Self::Id(EntityId(id))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct CreationId(pub u64);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn entity_ref_name_serializes_as_string() {
let r = EntityRef::Name("campfire".to_string());
let json = serde_json::to_string(&r).unwrap();
assert_eq!(json, r#""campfire""#);
}
#[test]
fn entity_ref_id_serializes_as_object() {
let r = EntityRef::Id(EntityId(42));
let json = serde_json::to_string(&r).unwrap();
assert!(json.contains("42"));
}
#[test]
fn entity_name_display() {
let name = EntityName::new("sun_lamp");
assert_eq!(format!("{}", name), "sun_lamp");
}
}