use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::time::{SystemTime, UNIX_EPOCH};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Event {
#[serde(rename = "type")]
pub event_type: String,
pub data: Value,
pub ts: u64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub id: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub actor: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub meta: Option<Value>,
}
impl Event {
pub fn new(event_type: &str, data: Value) -> Self {
let ts = SystemTime::now()
.duration_since(UNIX_EPOCH)
.expect("system clock is before Unix epoch")
.as_secs();
Event {
event_type: event_type.to_string(),
data,
ts,
id: None,
actor: None,
meta: None,
}
}
pub fn with_id(mut self, id: impl Into<String>) -> Self {
self.id = Some(id.into());
self
}
pub fn with_actor(mut self, actor: impl Into<String>) -> Self {
self.actor = Some(actor.into());
self
}
pub fn with_meta(mut self, meta: Value) -> Self {
self.meta = Some(meta);
self
}
}