use std::time::Instant;
#[derive(Debug, Clone)]
pub struct ChangeEvent<Id> {
pub entity_id: Id,
pub property_key: &'static str,
pub timestamp: Instant,
}
impl<Id> ChangeEvent<Id> {
pub fn new(entity_id: Id, property_key: &'static str) -> Self {
Self {
entity_id,
property_key,
timestamp: Instant::now(),
}
}
pub fn with_timestamp(entity_id: Id, property_key: &'static str, timestamp: Instant) -> Self {
Self {
entity_id,
property_key,
timestamp,
}
}
}
impl<Id: PartialEq> PartialEq for ChangeEvent<Id> {
fn eq(&self, other: &Self) -> bool {
self.entity_id == other.entity_id && self.property_key == other.property_key
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_change_event_creation() {
let event = ChangeEvent::new("entity-1".to_string(), "temperature");
assert_eq!(event.entity_id, "entity-1");
assert_eq!(event.property_key, "temperature");
}
#[test]
fn test_change_event_equality() {
let event1 = ChangeEvent::new("entity-1".to_string(), "temperature");
let event2 = ChangeEvent::new("entity-1".to_string(), "temperature");
let event3 = ChangeEvent::new("entity-2".to_string(), "temperature");
let event4 = ChangeEvent::new("entity-1".to_string(), "humidity");
assert_eq!(event1, event2);
assert_ne!(event1, event3);
assert_ne!(event1, event4);
}
}