pjson-rs 0.5.1

Priority JSON Streaming Protocol - high-performance priority-based JSON streaming (requires nightly Rust)
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//! Event service for handling domain events with DTO serialization
//!
//! This service provides a clean boundary between domain events and their
//! serialized representations, maintaining Clean Architecture principles.

use crate::{
    application::{
        ApplicationResult,
        dto::{DomainEventDto, FromDto, ToDto},
    },
    domain::{
        events::{DomainEvent, EventStore},
        value_objects::{SessionId, StreamId},
    },
};
use chrono::{DateTime, Utc};
use std::sync::{Arc, Mutex};
use tracing;

/// Service for managing domain events with DTO conversion
/// Uses compile-time polymorphism for zero-cost abstractions
pub struct EventService<S, H>
where
    S: EventStore,
    H: EventHandler,
{
    event_store: Arc<Mutex<S>>,
    event_handler: H,
}

/// Zero-cost event handler trait with stack allocation
pub trait EventHandler {
    type HandleFuture<'a>: std::future::Future<Output = ApplicationResult<()>> + Send + 'a
    where
        Self: 'a;

    fn handle_event(&self, event: &DomainEvent) -> Self::HandleFuture<'_>;
}

/// No-op handler for when events don't need processing
pub struct NoOpEventHandler;

impl EventHandler for NoOpEventHandler {
    type HandleFuture<'a>
        = impl std::future::Future<Output = ApplicationResult<()>> + Send + 'a
    where
        Self: 'a;

    fn handle_event(&self, _event: &DomainEvent) -> Self::HandleFuture<'_> {
        async move { Ok(()) }
    }
}

/// Logging handler with zero allocations
pub struct LoggingEventHandler;

impl EventHandler for LoggingEventHandler {
    type HandleFuture<'a>
        = impl std::future::Future<Output = ApplicationResult<()>> + Send + 'a
    where
        Self: 'a;

    fn handle_event(&self, event: &DomainEvent) -> Self::HandleFuture<'_> {
        let event_type = event.event_type();
        let session_id = event.session_id();
        let stream_id = event.stream_id();

        async move {
            // Stack-allocated logging without heap allocation
            match event_type {
                "stream_completed" => {
                    if let Some(stream) = stream_id {
                        tracing::info!(
                            "Stream completed: session={}, stream={}",
                            session_id,
                            stream
                        );
                    }
                }
                "session_activated" => {
                    tracing::info!("Session activated: {}", session_id);
                }
                _ => {
                    tracing::debug!("Domain event processed: {}", event_type);
                }
            }
            Ok(())
        }
    }
}

impl<S, H> EventService<S, H>
where
    S: EventStore,
    H: EventHandler,
{
    /// Create new event service with event store and handler
    pub fn new(event_store: Arc<Mutex<S>>, event_handler: H) -> Self {
        Self {
            event_store,
            event_handler,
        }
    }

    /// Publish domain event with zero-cost handling
    pub async fn publish_event(&self, event: DomainEvent) -> ApplicationResult<()> {
        // Handle event with zero-cost abstraction
        self.event_handler.handle_event(&event).await?;

        // Store event
        self.event_store
            .lock()
            .map_err(|_| {
                crate::application::ApplicationError::Logic(
                    "Failed to acquire event store lock".to_string(),
                )
            })?
            .append_events(vec![event])
            .map_err(crate::application::ApplicationError::Logic)?;

        Ok(())
    }

    /// Publish multiple domain events
    pub async fn publish_events(&self, events: Vec<DomainEvent>) -> ApplicationResult<()> {
        // Handle events with zero-cost abstraction
        for event in &events {
            self.event_handler.handle_event(event).await?;
        }

        // Store all events
        self.event_store
            .lock()
            .map_err(|_| {
                crate::application::ApplicationError::Logic(
                    "Failed to acquire event store lock".to_string(),
                )
            })?
            .append_events(events)
            .map_err(crate::application::ApplicationError::Logic)?;

        Ok(())
    }

    /// Get events for session as DTOs (for API responses)
    pub fn get_session_events_dto(
        &self,
        session_id: SessionId,
    ) -> ApplicationResult<Vec<DomainEventDto>> {
        let events = self
            .event_store
            .lock()
            .map_err(|_| {
                crate::application::ApplicationError::Logic(
                    "Failed to acquire event store lock".to_string(),
                )
            })?
            .get_events_for_session(session_id)
            .map_err(crate::application::ApplicationError::Logic)?;

        Ok(events.into_iter().map(|e| e.to_dto()).collect())
    }

    /// Get events for stream as DTOs (for API responses)
    pub fn get_stream_events_dto(
        &self,
        stream_id: StreamId,
    ) -> ApplicationResult<Vec<DomainEventDto>> {
        let events = self
            .event_store
            .lock()
            .map_err(|_| {
                crate::application::ApplicationError::Logic(
                    "Failed to acquire event store lock".to_string(),
                )
            })?
            .get_events_for_stream(stream_id)
            .map_err(crate::application::ApplicationError::Logic)?;

        Ok(events.into_iter().map(|e| e.to_dto()).collect())
    }

    /// Get events since timestamp as DTOs (for API responses)
    pub fn get_events_since_dto(
        &self,
        since: DateTime<Utc>,
    ) -> ApplicationResult<Vec<DomainEventDto>> {
        let events = self
            .event_store
            .lock()
            .map_err(|_| {
                crate::application::ApplicationError::Logic(
                    "Failed to acquire event store lock".to_string(),
                )
            })?
            .get_events_since(since)
            .map_err(crate::application::ApplicationError::Logic)?;

        Ok(events.into_iter().map(|e| e.to_dto()).collect())
    }

    /// Get events for session (domain objects, for internal use)
    pub fn get_session_events(&self, session_id: SessionId) -> ApplicationResult<Vec<DomainEvent>> {
        self.event_store
            .lock()
            .map_err(|_| {
                crate::application::ApplicationError::Logic(
                    "Failed to acquire event store lock".to_string(),
                )
            })?
            .get_events_for_session(session_id)
            .map_err(crate::application::ApplicationError::Logic)
    }

    /// Get events for stream (domain objects, for internal use)
    pub fn get_stream_events(&self, stream_id: StreamId) -> ApplicationResult<Vec<DomainEvent>> {
        self.event_store
            .lock()
            .map_err(|_| {
                crate::application::ApplicationError::Logic(
                    "Failed to acquire event store lock".to_string(),
                )
            })?
            .get_events_for_stream(stream_id)
            .map_err(crate::application::ApplicationError::Logic)
    }

    /// Replay events from DTOs (for event sourcing reconstruction)
    pub fn replay_from_dtos(
        &self,
        event_dtos: Vec<DomainEventDto>,
    ) -> ApplicationResult<Vec<DomainEvent>> {
        let mut events = Vec::new();

        for dto in event_dtos {
            let event =
                DomainEvent::from_dto(dto).map_err(crate::application::ApplicationError::Domain)?;
            events.push(event);
        }

        Ok(events)
    }
}

/// Convenience constructors for common configurations
impl<S> EventService<S, NoOpEventHandler>
where
    S: EventStore,
{
    /// Create event service with no-op handler (maximum performance)
    pub fn with_noop_handler(event_store: Arc<Mutex<S>>) -> Self {
        Self::new(event_store, NoOpEventHandler)
    }
}

impl<S> EventService<S, LoggingEventHandler>
where
    S: EventStore,
{
    /// Create event service with logging handler (zero-allocation logging)
    pub fn with_logging_handler(event_store: Arc<Mutex<S>>) -> Self {
        Self::new(event_store, LoggingEventHandler)
    }
}

/// Event publishing convenience methods
impl<S, H> EventService<S, H>
where
    S: EventStore,
    H: EventHandler,
{
    /// Publish session activated event  
    pub async fn publish_session_activated(&self, session_id: SessionId) -> ApplicationResult<()> {
        let event = DomainEvent::SessionActivated {
            session_id,
            timestamp: Utc::now(),
        };
        self.publish_event(event).await
    }

    /// Publish session closed event
    pub async fn publish_session_closed(&self, session_id: SessionId) -> ApplicationResult<()> {
        let event = DomainEvent::SessionClosed {
            session_id,
            timestamp: Utc::now(),
        };
        self.publish_event(event).await
    }

    /// Publish stream created event
    pub async fn publish_stream_created(
        &self,
        session_id: SessionId,
        stream_id: StreamId,
    ) -> ApplicationResult<()> {
        let event = DomainEvent::StreamCreated {
            session_id,
            stream_id,
            timestamp: Utc::now(),
        };
        self.publish_event(event).await
    }

    /// Publish stream completed event
    pub async fn publish_stream_completed(
        &self,
        session_id: SessionId,
        stream_id: StreamId,
    ) -> ApplicationResult<()> {
        let event = DomainEvent::StreamCompleted {
            session_id,
            stream_id,
            timestamp: Utc::now(),
        };
        self.publish_event(event).await
    }

    /// Publish stream failed event
    pub async fn publish_stream_failed(
        &self,
        session_id: SessionId,
        stream_id: StreamId,
        error: String,
    ) -> ApplicationResult<()> {
        let event = DomainEvent::StreamFailed {
            session_id,
            stream_id,
            error,
            timestamp: Utc::now(),
        };
        self.publish_event(event).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::domain::{
        events::{EventSubscriber, InMemoryEventStore},
        value_objects::{SessionId, StreamId},
    };

    // Mock subscriber for testing
    #[allow(dead_code)] // Future test utility
    struct MockSubscriber {
        received_events: std::sync::Mutex<Vec<DomainEvent>>,
    }

    #[allow(dead_code)] // Future test utility
    impl MockSubscriber {
        fn new() -> Self {
            Self {
                received_events: std::sync::Mutex::new(Vec::new()),
            }
        }

        fn event_count(&self) -> usize {
            self.received_events
                .lock()
                .map(|events| events.len())
                .unwrap_or(0)
        }
    }

    impl EventSubscriber for MockSubscriber {
        type HandleFuture<'a>
            = impl std::future::Future<Output = crate::domain::DomainResult<()>> + Send + 'a
        where
            Self: 'a;

        fn handle(&self, event: &DomainEvent) -> Self::HandleFuture<'_> {
            let event = event.clone();
            async move {
                self.received_events
                    .lock()
                    .map_err(|_| {
                        crate::domain::DomainError::Logic(
                            "Event subscriber lock poisoned".to_string(),
                        )
                    })?
                    .push(event);
                Ok(())
            }
        }
    }

    #[tokio::test]
    async fn test_event_service_creation() {
        let store = Arc::new(std::sync::Mutex::new(InMemoryEventStore::new()));
        let _service = EventService::with_noop_handler(store);

        // Service created successfully
    }

    #[tokio::test]
    async fn test_publish_event() {
        let store = Arc::new(std::sync::Mutex::new(InMemoryEventStore::new()));
        let service = EventService::with_logging_handler(store.clone());

        let session_id = SessionId::new();

        service.publish_session_activated(session_id).await.unwrap();

        // Verify event was stored
        let events = service.get_session_events(session_id).unwrap();
        assert_eq!(events.len(), 1);

        match &events[0] {
            DomainEvent::SessionActivated {
                session_id: stored_id,
                ..
            } => {
                assert_eq!(*stored_id, session_id);
            }
            _ => panic!("Expected SessionActivated event"),
        }
    }

    #[tokio::test]
    async fn test_event_handler() {
        let store = Arc::new(std::sync::Mutex::new(InMemoryEventStore::new()));
        let service = EventService::with_logging_handler(store);

        let session_id = SessionId::new();
        service.publish_session_activated(session_id).await.unwrap();

        // Verify event was handled and stored
        let events = service.get_session_events(session_id).unwrap();
        assert_eq!(events.len(), 1);
    }

    #[tokio::test]
    async fn test_dto_conversion() {
        let store = Arc::new(std::sync::Mutex::new(InMemoryEventStore::new()));
        let service = EventService::with_logging_handler(store);

        let session_id = SessionId::new();
        let stream_id = StreamId::new();

        service
            .publish_stream_created(session_id, stream_id)
            .await
            .unwrap();

        // Get events as DTOs
        let event_dtos = service.get_session_events_dto(session_id).unwrap();
        assert_eq!(event_dtos.len(), 1);

        // Verify DTO structure
        match &event_dtos[0] {
            DomainEventDto::StreamCreated {
                session_id: dto_session_id,
                stream_id: dto_stream_id,
                ..
            } => {
                assert_eq!(dto_session_id.uuid(), session_id.as_uuid());
                assert_eq!(dto_stream_id.uuid(), stream_id.as_uuid());
            }
            _ => panic!("Expected StreamCreated DTO"),
        }

        // Test replay from DTOs
        let replayed = service.replay_from_dtos(event_dtos).unwrap();
        assert_eq!(replayed.len(), 1);
    }

    #[tokio::test]
    async fn test_multiple_events() {
        let store = Arc::new(std::sync::Mutex::new(InMemoryEventStore::new()));
        let service = EventService::with_logging_handler(store);

        let session_id = SessionId::new();
        let stream_id = StreamId::new();

        let events = vec![
            DomainEvent::SessionActivated {
                session_id,
                timestamp: Utc::now(),
            },
            DomainEvent::StreamCreated {
                session_id,
                stream_id,
                timestamp: Utc::now(),
            },
            DomainEvent::StreamStarted {
                session_id,
                stream_id,
                timestamp: Utc::now(),
            },
        ];

        service.publish_events(events).await.unwrap();

        let stored_events = service.get_session_events(session_id).unwrap();
        assert_eq!(stored_events.len(), 3);
    }
}