eventuary-core 0.1.0

Core event model and async IO traits for eventuary
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
use std::collections::HashMap;

use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::error::{Error, Result};
use crate::event::{Event, EventId};
use crate::event_key::EventKey;
use crate::metadata::Metadata;
use crate::namespace::Namespace;
use crate::organization::OrganizationId;
use crate::payload::{ContentType, Payload};
use crate::topic::Topic;

/// Wire-format representation of an [`Event`]. Field order matches
/// `Event` so the JSON shape is predictable and self-documenting.
///
/// `id` and `parent_id` carry `Uuid` directly so backends with native
/// UUID columns can bind/fetch without a String round-trip.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializedEvent {
    pub id: Uuid,
    pub organization: String,
    pub namespace: String,
    pub topic: String,
    pub key: String,
    pub payload: SerializedPayload,
    pub metadata: HashMap<String, String>,
    pub timestamp: DateTime<Utc>,
    pub version: u64,
    #[serde(default)]
    pub parent_id: Option<Uuid>,
    #[serde(default)]
    pub correlation_id: Option<String>,
    #[serde(default)]
    pub causation_id: Option<String>,
}

/// Wire-format representation of a [`Payload`].
///
/// Each variant carries the payload bytes in the natural shape for its
/// content type:
/// - `Json` carries the parsed `serde_json::Value` so the wire format
///   stays human-readable and `jq`-friendly.
/// - `PlainText` carries the text as a raw JSON string.
/// - `Binary` carries the raw bytes base64-encoded.
///
/// The wire tag is the content-type string, so the JSON shape is
/// `{"content_type": "...", "data": ...}` regardless of variant.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "content_type", content = "data")]
pub enum SerializedPayload {
    #[serde(rename = "application/json")]
    Json(serde_json::Value),
    #[serde(rename = "text/plain")]
    PlainText(String),
    #[serde(rename = "application/octet-stream")]
    Binary(#[serde(with = "base64_bytes")] Vec<u8>),
}

impl SerializedPayload {
    pub fn content_type(&self) -> ContentType {
        match self {
            Self::Json(_) => ContentType::Json,
            Self::PlainText(_) => ContentType::PlainText,
            Self::Binary(_) => ContentType::Binary,
        }
    }

    pub fn from_payload(payload: &Payload) -> Result<Self> {
        match payload.content_type() {
            ContentType::Json => {
                let value = serde_json::from_slice(payload.data())
                    .map_err(|e| Error::Serialization(e.to_string()))?;
                Ok(Self::Json(value))
            }
            ContentType::PlainText => {
                let text = std::str::from_utf8(payload.data())
                    .map_err(|e| Error::Serialization(e.to_string()))?;
                Ok(Self::PlainText(text.to_owned()))
            }
            ContentType::Binary => Ok(Self::Binary(payload.data().to_vec())),
        }
    }

    pub fn into_payload(self) -> Result<Payload> {
        match self {
            Self::Json(value) => {
                let bytes =
                    serde_json::to_vec(&value).map_err(|e| Error::Serialization(e.to_string()))?;
                Ok(Payload::from_raw(bytes, ContentType::Json))
            }
            Self::PlainText(text) => Ok(Payload::from_string(text)),
            Self::Binary(bytes) => Ok(Payload::from_bytes(bytes)),
        }
    }
}

mod base64_bytes {
    use base64::Engine;
    use base64::engine::general_purpose::STANDARD as BASE64;
    use serde::{Deserialize, Deserializer, Serializer};

    pub(super) fn serialize<S: Serializer>(bytes: &[u8], s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(&BASE64.encode(bytes))
    }

    pub(super) fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
        let encoded = String::deserialize(d)?;
        BASE64.decode(encoded).map_err(serde::de::Error::custom)
    }
}

impl SerializedEvent {
    pub fn from_event(event: &Event<Payload>) -> Result<Self> {
        Ok(Self {
            id: *event.id().as_uuid(),
            organization: event.organization().to_string(),
            namespace: event.namespace().to_string(),
            topic: event.topic().to_string(),
            key: event.key().to_string(),
            payload: SerializedPayload::from_payload(event.payload())?,
            metadata: event
                .metadata()
                .as_map()
                .iter()
                .map(|(k, v)| (k.clone(), v.clone()))
                .collect(),
            timestamp: event.timestamp(),
            version: event.version(),
            parent_id: event.parent_id().map(|id| *id.as_uuid()),
            correlation_id: event.correlation_id().map(|id| id.to_string()),
            causation_id: event.causation_id().map(|id| id.to_string()),
        })
    }

    pub fn to_event(&self) -> Result<Event<Payload>> {
        let key = EventKey::new(&self.key)?;
        let payload = self.payload.clone().into_payload()?;
        let parent_id = self.parent_id.map(EventId::from_uuid);
        let correlation_id = self
            .correlation_id
            .as_deref()
            .map(EventKey::new)
            .transpose()?;
        let causation_id = self
            .causation_id
            .as_deref()
            .map(EventKey::new)
            .transpose()?;

        Event::new(
            EventId::from_uuid(self.id),
            OrganizationId::new(&self.organization)?,
            Namespace::new(&self.namespace)?,
            Topic::new(&self.topic)?,
            key,
            payload,
            Metadata::try_from(self.metadata.clone())?,
            self.timestamp,
            self.version,
            parent_id,
            correlation_id,
            causation_id,
        )
    }

    pub fn to_json_value(&self) -> serde_json::Value {
        serde_json::to_value(self).expect("SerializedEvent must serialize to JSON")
    }

    pub fn from_json_value(value: serde_json::Value) -> Result<Self> {
        serde_json::from_value(value).map_err(|e| Error::Serialization(e.to_string()))
    }

    pub fn to_json_string(&self) -> Result<String> {
        serde_json::to_string(self).map_err(|e| Error::Serialization(e.to_string()))
    }

    pub fn from_json_str(s: &str) -> Result<Self> {
        serde_json::from_str(s).map_err(|e| Error::Serialization(e.to_string()))
    }

    pub fn from_json_slice(bytes: &[u8]) -> Result<Self> {
        let value =
            serde_json::from_slice(bytes).map_err(|e| Error::Serialization(e.to_string()))?;
        Self::from_json_value(value)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn event_with_key(payload: Payload, key: &str) -> Event {
        Event::builder("acme", "/x", "thing.happened", key, payload)
            .unwrap()
            .build()
            .expect("valid event")
    }

    #[test]
    fn roundtrip() {
        let payload = Payload::from_json(&serde_json::json!({"key": "value"})).unwrap();
        let event = Event::builder("acme", "/task", "task.created", "task-123", payload)
            .unwrap()
            .build()
            .unwrap();

        let serialized = SerializedEvent::from_event(&event).unwrap();
        assert_eq!(serialized.topic, "task.created");
        assert_eq!(serialized.namespace, "/task");
        assert_eq!(serialized.organization, "acme");
        assert_eq!(serialized.key.as_str(), "task-123");

        let restored = serialized.to_event().unwrap();
        assert_eq!(restored.topic().as_str(), "task.created");
        assert_eq!(restored.id(), event.id());
        assert_eq!(restored.key().as_str(), "task-123");
    }

    #[test]
    fn field_order_matches_event() {
        let event = Event::builder(
            "acme",
            "/x",
            "thing.happened",
            "k",
            Payload::from_string("p"),
        )
        .unwrap()
        .build()
        .unwrap();
        let serialized = SerializedEvent::from_event(&event).unwrap();
        let json = serialized.to_json_string().unwrap();
        let id_pos = json.find("\"id\"").unwrap();
        let org_pos = json.find("\"organization\"").unwrap();
        let ns_pos = json.find("\"namespace\"").unwrap();
        let topic_pos = json.find("\"topic\"").unwrap();
        let key_pos = json.find("\"key\"").unwrap();
        let payload_pos = json.find("\"payload\"").unwrap();
        let metadata_pos = json.find("\"metadata\"").unwrap();
        let timestamp_pos = json.find("\"timestamp\"").unwrap();
        let version_pos = json.find("\"version\"").unwrap();
        assert!(id_pos < org_pos);
        assert!(org_pos < ns_pos);
        assert!(ns_pos < topic_pos);
        assert!(topic_pos < key_pos);
        assert!(key_pos < payload_pos);
        assert!(payload_pos < metadata_pos);
        assert!(metadata_pos < timestamp_pos);
        assert!(timestamp_pos < version_pos);
    }

    #[test]
    fn plain_text_payload_is_a_raw_json_string() {
        let event = event_with_key(Payload::from_string("hello world"), "k");
        let serialized = SerializedEvent::from_event(&event).unwrap();
        match &serialized.payload {
            SerializedPayload::PlainText(text) => assert_eq!(text, "hello world"),
            other => panic!("expected PlainText, got {other:?}"),
        }
        let json = serialized.to_json_string().unwrap();
        assert!(json.contains("\"content_type\":\"text/plain\""));
        assert!(json.contains("\"data\":\"hello world\""));

        let restored = serialized.to_event().unwrap();
        assert_eq!(restored.payload().data(), b"hello world");
        assert_eq!(restored.payload().content_type(), ContentType::PlainText);
    }

    #[test]
    fn binary_payload_is_base64_in_data_field() {
        let bytes = vec![0xff, 0x00, 0x01, 0xfe, 0x80, 0x7f, 0x10];
        let event = event_with_key(Payload::from_bytes(bytes.clone()), "k");
        let serialized = SerializedEvent::from_event(&event).unwrap();
        match &serialized.payload {
            SerializedPayload::Binary(b) => assert_eq!(b, &bytes),
            other => panic!("expected Binary, got {other:?}"),
        }
        let json = serialized.to_json_string().unwrap();
        assert!(json.contains("\"content_type\":\"application/octet-stream\""));

        let restored = serialized.to_event().unwrap();
        assert_eq!(restored.payload().data(), bytes.as_slice());
        assert_eq!(restored.payload().content_type(), ContentType::Binary);
    }

    #[test]
    fn json_payload_stays_human_readable() {
        let event = event_with_key(
            Payload::from_json(&serde_json::json!({"k": "v"})).unwrap(),
            "k",
        );
        let serialized = SerializedEvent::from_event(&event).unwrap();
        match &serialized.payload {
            SerializedPayload::Json(value) => {
                assert_eq!(value, &serde_json::json!({"k": "v"}));
            }
            other => panic!("expected Json, got {other:?}"),
        }
        let json = serialized.to_json_string().unwrap();
        assert!(json.contains("\"content_type\":\"application/json\""));
        assert!(json.contains("\"data\":{\"k\":\"v\"}"));
    }

    #[test]
    fn json_value_round_trip() {
        let event = Event::builder(
            "acme",
            "/task",
            "task.created",
            "task-123",
            Payload::from_json(&serde_json::json!({"key": "value"})).unwrap(),
        )
        .unwrap()
        .build()
        .unwrap();

        let serialized = SerializedEvent::from_event(&event).unwrap();
        let value = serialized.to_json_value();
        let parsed = SerializedEvent::from_json_value(value).unwrap();

        assert_eq!(parsed.id, serialized.id);
        assert_eq!(parsed.topic, serialized.topic);
        assert_eq!(parsed.payload.content_type(), ContentType::Json);
    }

    #[test]
    fn json_string_round_trip() {
        let event = Event::builder(
            "acme",
            "/task",
            "task.created",
            "task-123",
            Payload::from_json(&serde_json::json!({"key": "value"})).unwrap(),
        )
        .unwrap()
        .build()
        .unwrap();

        let serialized = SerializedEvent::from_event(&event).unwrap();
        let s = serialized.to_json_string().unwrap();
        let parsed = SerializedEvent::from_json_str(&s).unwrap();

        assert_eq!(parsed.id, serialized.id);
        assert_eq!(parsed.namespace, serialized.namespace);
        assert_eq!(parsed.organization, serialized.organization);
    }

    #[test]
    fn from_json_slice_roundtrip() {
        let event = Event::builder(
            "acme",
            "/task",
            "task.created",
            "task-123",
            Payload::from_json(&serde_json::json!({"key": "value"})).unwrap(),
        )
        .unwrap()
        .build()
        .unwrap();

        let serialized = SerializedEvent::from_event(&event).unwrap();
        let bytes = serialized.to_json_string().unwrap().into_bytes();
        let parsed = SerializedEvent::from_json_slice(&bytes).unwrap();

        assert_eq!(parsed.id, serialized.id);
        assert_eq!(parsed.topic, serialized.topic);
        assert_eq!(parsed.payload.content_type(), ContentType::Json);
    }

    #[test]
    fn json_string_round_trip_with_binary() {
        let bytes = vec![0xde, 0xad, 0xbe, 0xef];
        let event = event_with_key(Payload::from_bytes(bytes.clone()), "b1");

        let serialized = SerializedEvent::from_event(&event).unwrap();
        let s = serialized.to_json_string().unwrap();
        let parsed = SerializedEvent::from_json_str(&s).unwrap();
        let restored = parsed.to_event().unwrap();

        assert_eq!(restored.payload().data(), bytes.as_slice());
    }

    #[test]
    fn lineage_fields_roundtrip() {
        let parent_id = EventId::new();
        let event = Event::builder(
            "acme",
            "/x",
            "thing.happened",
            "k",
            Payload::from_string("p"),
        )
        .unwrap()
        .parent_id(parent_id)
        .correlation_id("corr")
        .unwrap()
        .causation_id("cause")
        .unwrap()
        .build()
        .unwrap();

        let serialized = SerializedEvent::from_event(&event).unwrap();
        assert_eq!(serialized.key.as_str(), "k");
        assert_eq!(serialized.parent_id, Some(*parent_id.as_uuid()));
        assert_eq!(serialized.correlation_id.as_deref(), Some("corr"));
        assert_eq!(serialized.causation_id.as_deref(), Some("cause"));

        let restored = serialized.to_event().unwrap();
        assert_eq!(restored.key().as_str(), "k");
        assert_eq!(restored.parent_id(), Some(parent_id));
        assert_eq!(
            restored.correlation_id().map(EventKey::as_str),
            Some("corr")
        );
        assert_eq!(restored.causation_id().map(EventKey::as_str), Some("cause"));
    }

    #[test]
    fn serialized_event_rejects_missing_key() {
        let value = serde_json::json!({
            "id": uuid::Uuid::now_v7(),
            "organization": "acme",
            "namespace": "/task",
            "topic": "task.created",
            "payload": {"content_type": "text/plain", "data": "hello"},
            "metadata": {},
            "timestamp": chrono::Utc::now(),
            "version": 1
        });

        let err = SerializedEvent::from_json_value(value).unwrap_err();
        assert!(matches!(err, Error::Serialization(_)));
    }
}