pub struct Event {
pub event_type: String,
pub data: Value,
pub ts: u64,
pub id: Option<String>,
pub actor: Option<String>,
pub meta: Option<Value>,
}Expand description
An immutable event record stored in the log.
Events are serialized as single JSON lines in app.jsonl. The data field
is intentionally untyped (serde_json::Value) — the log has no opinion
about event shapes. Reducers give events meaning.
Optional metadata fields (id, actor, meta) support multi-user
applications, audit trails, and event correlation. When None, these
fields are omitted from serialized output — existing logs without
metadata deserialize without error.
§Examples
use eventfold::Event;
use serde_json::json;
// Simple event — no metadata
let event = Event::new("user_clicked", json!({"button": "submit"}));
assert_eq!(event.event_type, "user_clicked");
assert!(event.ts > 0);
// With metadata
let event = Event::new("order_placed", json!({"total": 99.99}))
.with_id("ord-001")
.with_actor("user_42");
assert_eq!(event.id, Some("ord-001".to_string()));
assert_eq!(event.actor, Some("user_42".to_string()));Fields§
§event_type: StringThe event type identifier (e.g. "todo_added", "user_clicked").
Serialized as "type" in JSON for brevity.
data: ValueArbitrary JSON payload. The log does not validate this — reducers interpret it however they need.
ts: u64Unix timestamp in seconds, auto-populated by Event::new.
id: Option<String>Unique event identifier.
Not auto-generated — callers provide their own (uuid, ulid, etc.)
or leave as None for simple use cases.
actor: Option<String>Identity of the actor that caused this event (user ID, service name, API key, etc.). Interpretation is application-defined.
meta: Option<Value>Extensible metadata bag for cross-cutting concerns.
Typical keys: "session", "correlation_id", "source",
"schema_version". Kept separate from data so domain payloads
stay clean.
Implementations§
Source§impl Event
impl Event
Sourcepub fn new(event_type: &str, data: Value) -> Self
pub fn new(event_type: &str, data: Value) -> Self
Create a new event with the given type and data.
The timestamp is set to the current time (seconds since Unix epoch).
Metadata fields (id, actor, meta) default to None — use the
builder methods to set them.
§Panics
Panics if the system clock is set before the Unix epoch.
§Examples
use eventfold::Event;
use serde_json::json;
let event = Event::new("page_view", json!({"url": "/home"}));
assert_eq!(event.event_type, "page_view");
assert_eq!(event.data["url"], "/home");
assert_eq!(event.id, None);
assert_eq!(event.actor, None);
assert_eq!(event.meta, None);Sourcepub fn with_id(self, id: impl Into<String>) -> Self
pub fn with_id(self, id: impl Into<String>) -> Self
Set the event’s unique identifier.
§Examples
use eventfold::Event;
use serde_json::json;
let event = Event::new("click", json!({})).with_id("evt-123");
assert_eq!(event.id, Some("evt-123".to_string()));Sourcepub fn with_actor(self, actor: impl Into<String>) -> Self
pub fn with_actor(self, actor: impl Into<String>) -> Self
Set the actor that caused this event.
§Examples
use eventfold::Event;
use serde_json::json;
let event = Event::new("click", json!({})).with_actor("user_42");
assert_eq!(event.actor, Some("user_42".to_string()));