this-rs 0.0.9

Framework for building complex multi-entity REST and GraphQL APIs with many relationships
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//! WebSocket message protocol definitions
//!
//! Defines the JSON messages exchanged between WebSocket clients and the server.
//!
//! ## Client → Server Messages
//!
//! ```json
//! // Subscribe to events
//! {"type": "subscribe", "filter": {"entity_type": "order", "event_type": "created"}}
//!
//! // Unsubscribe
//! {"type": "unsubscribe", "subscription_id": "sub_abc123"}
//!
//! // Keepalive
//! {"type": "ping"}
//! ```
//!
//! ## Server → Client Messages
//!
//! ```json
//! // Event notification
//! {"type": "event", "subscription_id": "sub_abc123", "data": {...}}
//!
//! // Subscription confirmed
//! {"type": "subscribed", "subscription_id": "sub_abc123", "filter": {...}}
//!
//! // Unsubscription confirmed
//! {"type": "unsubscribed", "subscription_id": "sub_abc123"}
//!
//! // Keepalive response
//! {"type": "pong"}
//!
//! // Error
//! {"type": "error", "message": "Invalid subscription filter"}
//! ```

use crate::core::events::{EventEnvelope, FrameworkEvent};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use uuid::Uuid;

/// Messages sent from client to server
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ClientMessage {
    /// Subscribe to events matching a filter
    Subscribe {
        /// Filter criteria for events
        filter: SubscriptionFilter,
    },
    /// Unsubscribe from a specific subscription
    Unsubscribe {
        /// The subscription ID to remove
        subscription_id: String,
    },
    /// Keepalive ping
    Ping,
}

/// Messages sent from server to client
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ServerMessage {
    /// An event matching a subscription
    Event {
        /// Which subscription matched this event
        subscription_id: String,
        /// The event envelope with metadata
        data: EventEnvelope,
    },
    /// Subscription confirmation
    Subscribed {
        /// The assigned subscription ID
        subscription_id: String,
        /// The filter that was registered
        filter: SubscriptionFilter,
    },
    /// Unsubscription confirmation
    Unsubscribed {
        /// The subscription ID that was removed
        subscription_id: String,
    },
    /// Keepalive response
    Pong,
    /// Error message
    Error {
        /// Human-readable error description
        message: String,
    },
    /// Welcome message on connection
    Welcome {
        /// Unique connection ID
        connection_id: String,
    },
    /// A notification payload dispatched via the sink system
    ///
    /// Unlike `Event` (which is triggered by subscription filters), this message
    /// is pushed by the server-side sink pipeline (e.g., `deliver` operator).
    /// The client does NOT need to subscribe to receive these.
    Notification {
        /// The notification payload (built by the pipeline's `map` operator)
        data: Value,
    },
}

/// Filter criteria for event subscriptions
///
/// All fields are optional. When a field is `None`, it acts as a wildcard
/// (matches everything). When set, only events matching that field are delivered.
///
/// # Examples
///
/// Subscribe to all events:
/// ```json
/// {}
/// ```
///
/// Subscribe to all order events:
/// ```json
/// {"entity_type": "order"}
/// ```
///
/// Subscribe to a specific entity:
/// ```json
/// {"entity_type": "order", "entity_id": "550e8400-e29b-41d4-a716-446655440000"}
/// ```
///
/// Subscribe to all creation events:
/// ```json
/// {"event_type": "created"}
/// ```
///
/// Subscribe to order creations only:
/// ```json
/// {"entity_type": "order", "event_type": "created"}
/// ```
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SubscriptionFilter {
    /// Filter by entity type (e.g., "order", "invoice")
    /// None = match all entity types
    #[serde(skip_serializing_if = "Option::is_none")]
    pub entity_type: Option<String>,

    /// Filter by specific entity ID
    /// None = match all entities of the type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub entity_id: Option<Uuid>,

    /// Filter by event type: "created", "updated", "deleted"
    /// None = match all event types
    #[serde(skip_serializing_if = "Option::is_none")]
    pub event_type: Option<String>,

    /// Filter by event kind: "entity" or "link"
    /// None = match both entity and link events
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kind: Option<String>,
}

impl SubscriptionFilter {
    /// Check if an event matches this filter
    ///
    /// All fields act as AND conditions. A `None` field matches everything.
    pub fn matches(&self, event: &FrameworkEvent) -> bool {
        // Check kind filter
        if let Some(ref kind) = self.kind
            && event.event_kind() != kind
        {
            return false;
        }

        // Check entity_type filter
        if let Some(ref entity_type) = self.entity_type {
            match event.entity_type() {
                Some(et) if et == entity_type => {}
                Some(_) => return false,
                // Link events don't have entity_type — if filtering by entity_type, skip links
                None => return false,
            }
        }

        // Check entity_id filter
        if let Some(entity_id) = self.entity_id {
            match event.entity_id() {
                Some(eid) if eid == entity_id => {}
                Some(_) => return false,
                None => return false,
            }
        }

        // Check event_type (action) filter
        if let Some(ref event_type) = self.event_type
            && event.action() != event_type
        {
            return false;
        }

        true
    }
}

/// A subscription with its filter and a unique ID
#[derive(Debug, Clone)]
pub struct Subscription {
    /// Unique subscription ID
    pub id: String,
    /// The filter for this subscription
    pub filter: SubscriptionFilter,
}

impl Subscription {
    /// Create a new subscription with a generated ID
    pub fn new(filter: SubscriptionFilter) -> Self {
        Self {
            id: format!("sub_{}", Uuid::new_v4().simple()),
            filter,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::events::{EntityEvent, LinkEvent};
    use serde_json::json;

    // === Serialization tests ===

    #[test]
    fn test_client_message_subscribe_serialization() {
        let msg = ClientMessage::Subscribe {
            filter: SubscriptionFilter {
                entity_type: Some("order".to_string()),
                entity_id: None,
                event_type: Some("created".to_string()),
                kind: None,
            },
        };

        let json = serde_json::to_value(&msg).unwrap();
        assert_eq!(json["type"], "subscribe");
        assert_eq!(json["filter"]["entity_type"], "order");
        assert_eq!(json["filter"]["event_type"], "created");
    }

    #[test]
    fn test_client_message_unsubscribe_serialization() {
        let msg = ClientMessage::Unsubscribe {
            subscription_id: "sub_123".to_string(),
        };

        let json = serde_json::to_value(&msg).unwrap();
        assert_eq!(json["type"], "unsubscribe");
        assert_eq!(json["subscription_id"], "sub_123");
    }

    #[test]
    fn test_client_message_ping_serialization() {
        let msg = ClientMessage::Ping;
        let json = serde_json::to_value(&msg).unwrap();
        assert_eq!(json["type"], "ping");
    }

    #[test]
    fn test_server_message_event_serialization() {
        let envelope = EventEnvelope::new(FrameworkEvent::Entity(EntityEvent::Created {
            entity_type: "order".to_string(),
            entity_id: Uuid::new_v4(),
            data: json!({"amount": 42}),
        }));

        let msg = ServerMessage::Event {
            subscription_id: "sub_123".to_string(),
            data: envelope,
        };

        let json = serde_json::to_value(&msg).unwrap();
        assert_eq!(json["type"], "event");
        assert_eq!(json["subscription_id"], "sub_123");
        assert!(json["data"]["event"].is_object());
    }

    #[test]
    fn test_server_message_pong_serialization() {
        let msg = ServerMessage::Pong;
        let json = serde_json::to_value(&msg).unwrap();
        assert_eq!(json["type"], "pong");
    }

    #[test]
    fn test_server_message_error_serialization() {
        let msg = ServerMessage::Error {
            message: "Something went wrong".to_string(),
        };

        let json = serde_json::to_value(&msg).unwrap();
        assert_eq!(json["type"], "error");
        assert_eq!(json["message"], "Something went wrong");
    }

    // === Deserialization round-trip tests ===

    #[test]
    fn test_client_message_subscribe_roundtrip() {
        let json_str =
            r#"{"type":"subscribe","filter":{"entity_type":"order","event_type":"created"}}"#;
        let msg: ClientMessage = serde_json::from_str(json_str).unwrap();

        match msg {
            ClientMessage::Subscribe { filter } => {
                assert_eq!(filter.entity_type.as_deref(), Some("order"));
                assert_eq!(filter.event_type.as_deref(), Some("created"));
                assert!(filter.entity_id.is_none());
                assert!(filter.kind.is_none());
            }
            _ => panic!("Expected Subscribe"),
        }
    }

    #[test]
    fn test_client_message_ping_roundtrip() {
        let json_str = r#"{"type":"ping"}"#;
        let msg: ClientMessage = serde_json::from_str(json_str).unwrap();
        assert!(matches!(msg, ClientMessage::Ping));
    }

    #[test]
    fn test_subscription_filter_empty_roundtrip() {
        let json_str = r#"{}"#;
        let filter: SubscriptionFilter = serde_json::from_str(json_str).unwrap();
        assert!(filter.entity_type.is_none());
        assert!(filter.entity_id.is_none());
        assert!(filter.event_type.is_none());
        assert!(filter.kind.is_none());
    }

    // === Filter matching tests ===

    #[test]
    fn test_filter_empty_matches_everything() {
        let filter = SubscriptionFilter::default();

        let event1 = FrameworkEvent::Entity(EntityEvent::Created {
            entity_type: "order".to_string(),
            entity_id: Uuid::new_v4(),
            data: json!({}),
        });

        let event2 = FrameworkEvent::Link(LinkEvent::Created {
            link_type: "has_invoice".to_string(),
            link_id: Uuid::new_v4(),
            source_id: Uuid::new_v4(),
            target_id: Uuid::new_v4(),
            metadata: None,
        });

        assert!(filter.matches(&event1));
        assert!(filter.matches(&event2));
    }

    #[test]
    fn test_filter_by_entity_type() {
        let filter = SubscriptionFilter {
            entity_type: Some("order".to_string()),
            ..Default::default()
        };

        let order_event = FrameworkEvent::Entity(EntityEvent::Created {
            entity_type: "order".to_string(),
            entity_id: Uuid::new_v4(),
            data: json!({}),
        });

        let invoice_event = FrameworkEvent::Entity(EntityEvent::Created {
            entity_type: "invoice".to_string(),
            entity_id: Uuid::new_v4(),
            data: json!({}),
        });

        let link_event = FrameworkEvent::Link(LinkEvent::Created {
            link_type: "has_invoice".to_string(),
            link_id: Uuid::new_v4(),
            source_id: Uuid::new_v4(),
            target_id: Uuid::new_v4(),
            metadata: None,
        });

        assert!(filter.matches(&order_event));
        assert!(!filter.matches(&invoice_event));
        assert!(!filter.matches(&link_event)); // Links don't have entity_type
    }

    #[test]
    fn test_filter_by_entity_id() {
        let target_id = Uuid::new_v4();
        let other_id = Uuid::new_v4();

        let filter = SubscriptionFilter {
            entity_id: Some(target_id),
            ..Default::default()
        };

        let matching = FrameworkEvent::Entity(EntityEvent::Updated {
            entity_type: "order".to_string(),
            entity_id: target_id,
            data: json!({}),
        });

        let not_matching = FrameworkEvent::Entity(EntityEvent::Updated {
            entity_type: "order".to_string(),
            entity_id: other_id,
            data: json!({}),
        });

        assert!(filter.matches(&matching));
        assert!(!filter.matches(&not_matching));
    }

    #[test]
    fn test_filter_by_event_type() {
        let filter = SubscriptionFilter {
            event_type: Some("deleted".to_string()),
            ..Default::default()
        };

        let deleted = FrameworkEvent::Entity(EntityEvent::Deleted {
            entity_type: "order".to_string(),
            entity_id: Uuid::new_v4(),
        });

        let created = FrameworkEvent::Entity(EntityEvent::Created {
            entity_type: "order".to_string(),
            entity_id: Uuid::new_v4(),
            data: json!({}),
        });

        assert!(filter.matches(&deleted));
        assert!(!filter.matches(&created));
    }

    #[test]
    fn test_filter_by_kind() {
        let filter = SubscriptionFilter {
            kind: Some("link".to_string()),
            ..Default::default()
        };

        let entity_event = FrameworkEvent::Entity(EntityEvent::Created {
            entity_type: "order".to_string(),
            entity_id: Uuid::new_v4(),
            data: json!({}),
        });

        let link_event = FrameworkEvent::Link(LinkEvent::Created {
            link_type: "has_invoice".to_string(),
            link_id: Uuid::new_v4(),
            source_id: Uuid::new_v4(),
            target_id: Uuid::new_v4(),
            metadata: None,
        });

        assert!(!filter.matches(&entity_event));
        assert!(filter.matches(&link_event));
    }

    #[test]
    fn test_filter_combined_entity_type_and_action() {
        let filter = SubscriptionFilter {
            entity_type: Some("order".to_string()),
            event_type: Some("created".to_string()),
            ..Default::default()
        };

        let order_created = FrameworkEvent::Entity(EntityEvent::Created {
            entity_type: "order".to_string(),
            entity_id: Uuid::new_v4(),
            data: json!({}),
        });

        let order_deleted = FrameworkEvent::Entity(EntityEvent::Deleted {
            entity_type: "order".to_string(),
            entity_id: Uuid::new_v4(),
        });

        let invoice_created = FrameworkEvent::Entity(EntityEvent::Created {
            entity_type: "invoice".to_string(),
            entity_id: Uuid::new_v4(),
            data: json!({}),
        });

        assert!(filter.matches(&order_created));
        assert!(!filter.matches(&order_deleted));
        assert!(!filter.matches(&invoice_created));
    }

    #[test]
    fn test_subscription_generates_unique_id() {
        let sub1 = Subscription::new(SubscriptionFilter::default());
        let sub2 = Subscription::new(SubscriptionFilter::default());

        assert_ne!(sub1.id, sub2.id);
        assert!(sub1.id.starts_with("sub_"));
        assert!(sub2.id.starts_with("sub_"));
    }

    #[test]
    fn test_malformed_json_deserialization_error() {
        let malformed = r#"{"type": "subscribe", "filter": "not_an_object"}"#;
        let result = serde_json::from_str::<ClientMessage>(malformed);
        assert!(result.is_err(), "malformed JSON should fail to deserialize");
    }

    #[test]
    fn test_unknown_message_type_deserialization_error() {
        let unknown = r#"{"type": "unknown_action", "data": {}}"#;
        let result = serde_json::from_str::<ClientMessage>(unknown);
        assert!(
            result.is_err(),
            "unknown message type should fail to deserialize"
        );
    }

    #[test]
    fn test_missing_required_fields_deserialization_error() {
        // Subscribe requires a "filter" field
        let missing_filter = r#"{"type": "subscribe"}"#;
        let result = serde_json::from_str::<ClientMessage>(missing_filter);
        assert!(
            result.is_err(),
            "subscribe without filter should fail to deserialize"
        );

        // Unsubscribe requires a "subscription_id" field
        let missing_sub_id = r#"{"type": "unsubscribe"}"#;
        let result = serde_json::from_str::<ClientMessage>(missing_sub_id);
        assert!(
            result.is_err(),
            "unsubscribe without subscription_id should fail to deserialize"
        );
    }

    #[test]
    fn test_server_message_welcome_roundtrip() {
        let msg = ServerMessage::Welcome {
            connection_id: "conn_abc123".to_string(),
        };

        let json_str = serde_json::to_string(&msg).expect("Welcome should serialize");
        let deserialized: ServerMessage =
            serde_json::from_str(&json_str).expect("Welcome should deserialize");

        match deserialized {
            ServerMessage::Welcome { connection_id } => {
                assert_eq!(connection_id, "conn_abc123");
            }
            _ => panic!("Expected Welcome message"),
        }
    }

    #[test]
    fn test_server_message_subscribed_roundtrip() {
        let msg = ServerMessage::Subscribed {
            subscription_id: "sub_xyz789".to_string(),
            filter: SubscriptionFilter {
                entity_type: Some("invoice".to_string()),
                entity_id: None,
                event_type: Some("created".to_string()),
                kind: None,
            },
        };

        let json_str = serde_json::to_string(&msg).expect("Subscribed should serialize");
        let deserialized: ServerMessage =
            serde_json::from_str(&json_str).expect("Subscribed should deserialize");

        match deserialized {
            ServerMessage::Subscribed {
                subscription_id,
                filter,
            } => {
                assert_eq!(subscription_id, "sub_xyz789");
                assert_eq!(filter.entity_type.as_deref(), Some("invoice"));
                assert_eq!(filter.event_type.as_deref(), Some("created"));
                assert!(filter.entity_id.is_none());
                assert!(filter.kind.is_none());
            }
            _ => panic!("Expected Subscribed message"),
        }
    }

    #[test]
    fn test_server_message_notification_roundtrip() {
        let msg = ServerMessage::Notification {
            data: json!({"title": "New follower", "body": "Alice followed you"}),
        };

        let json_str = serde_json::to_string(&msg).expect("Notification should serialize");
        let deserialized: ServerMessage =
            serde_json::from_str(&json_str).expect("Notification should deserialize");

        match deserialized {
            ServerMessage::Notification { data } => {
                assert_eq!(data["title"], "New follower");
                assert_eq!(data["body"], "Alice followed you");
            }
            _ => panic!("Expected Notification message"),
        }

        // Verify the type tag
        let json_val: serde_json::Value = serde_json::from_str(&json_str).unwrap();
        assert_eq!(json_val["type"], "notification");
    }
}