//! Strongly-typed identifiers for shepherdd use serde::{Deserialize, Serialize}; use std::fmt; use uuid::Uuid; /// Unique identifier for an entry in the policy whitelist #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct EntryId(String); impl EntryId { pub fn new(id: impl Into) -> Self { Self(id.into()) } pub fn as_str(&self) -> &str { &self.0 } } impl fmt::Display for EntryId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) } } impl From for EntryId { fn from(s: String) -> Self { Self(s) } } impl From<&str> for EntryId { fn from(s: &str) -> Self { Self(s.to_string()) } } /// Unique identifier for a running session #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct SessionId(Uuid); impl SessionId { pub fn new() -> Self { Self(Uuid::new_v4()) } pub fn from_uuid(uuid: Uuid) -> Self { Self(uuid) } pub fn as_uuid(&self) -> &Uuid { &self.0 } } impl Default for SessionId { fn default() -> Self { Self::new() } } impl fmt::Display for SessionId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) } } /// Unique identifier for a connected IPC client #[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)] pub struct ClientId(Uuid); impl ClientId { pub fn new() -> Self { Self(Uuid::new_v4()) } pub fn from_uuid(uuid: Uuid) -> Self { Self(uuid) } } impl Default for ClientId { fn default() -> Self { Self::new() } } impl fmt::Display for ClientId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}", self.0) } } #[cfg(test)] mod tests { use super::*; #[test] fn entry_id_equality() { let id1 = EntryId::new("game-1"); let id2 = EntryId::new("game-1"); let id3 = EntryId::new("game-2"); assert_eq!(id1, id2); assert_ne!(id1, id3); } #[test] fn session_id_uniqueness() { let s1 = SessionId::new(); let s2 = SessionId::new(); assert_ne!(s1, s2); } #[test] fn ids_serialize_deserialize() { let entry_id = EntryId::new("test-entry"); let json = serde_json::to_string(&entry_id).unwrap(); let parsed: EntryId = serde_json::from_str(&json).unwrap(); assert_eq!(entry_id, parsed); let session_id = SessionId::new(); let json = serde_json::to_string(&session_id).unwrap(); let parsed: SessionId = serde_json::from_str(&json).unwrap(); assert_eq!(session_id, parsed); } }