Skip to main content

basilisk_rust_client/
protocol.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4
5/// Service-bus frame type constants.
6pub mod protocol_types {
7    /// Connect and authenticate a socket session.
8    pub const CONNECT: &str = "connect";
9
10    /// Subscribe to one or more topics.
11    pub const SUBSCRIBE: &str = "subscribe";
12    /// Unsubscribe from one or more topics.
13    pub const UNSUBSCRIBE: &str = "unsubscribe";
14    /// Publish an event envelope.
15    pub const PUBLISH: &str = "publish";
16    /// Send a forward request to another service.
17    pub const FORWARD: &str = "forward";
18    /// Receive a forward-response payload.
19    pub const FORWARD_RESPONSE: &str = "forward_response";
20    /// Event pushed from the bus.
21    pub const EVENT: &str = "event";
22    /// Command acknowledgment.
23    pub const ACK: &str = "ack";
24    /// Error response frame.
25    pub const ERROR: &str = "error";
26}
27
28/// Canonical event envelope transported over the Basilisk bus.
29#[derive(Debug, Serialize, Deserialize, Clone)]
30pub struct ServiceBusEventEnvelope {
31    /// Unique event identifier.
32    #[serde(rename = "eventId")]
33    pub event_id: String,
34    /// UTC timestamp when the event was emitted.
35    #[serde(rename = "emittedAtUtc")]
36    pub emitted_at_utc: DateTime<Utc>,
37    /// Origin service identifier.
38    #[serde(rename = "serviceId")]
39    pub service_id: String,
40    /// Origin service instance identifier.
41    #[serde(rename = "instanceId")]
42    pub instance_id: String,
43    /// Topic on which the event is routed.
44    pub topic: String,
45    /// Logical message type.
46    #[serde(rename = "messageType")]
47    pub message_type: String,
48    /// Correlation identifier used to link request/response chains.
49    #[serde(rename = "correlationId")]
50    pub correlation_id: i64,
51    /// Optional causation event id.
52    #[serde(rename = "causationId")]
53    pub causation_id: Option<String>,
54    /// Free-form JSON payload.
55    #[serde(default)]
56    pub payload: HashMap<String, serde_json::Value>,
57}
58
59/// Forward request payload embedded in a `forward` frame.
60#[derive(Debug, Serialize, Deserialize, Clone)]
61pub struct ServiceBusForwardRequest {
62    /// Destination service id.
63    #[serde(rename = "targetServiceId")]
64    pub target_service_id: String,
65    /// Message type to invoke on destination service.
66    #[serde(rename = "messageType")]
67    pub message_type: String,
68    /// Free-form request payload.
69    #[serde(default)]
70    pub payload: HashMap<String, serde_json::Value>,
71    /// Optional timeout override in milliseconds.
72    #[serde(rename = "timeoutMs")]
73    pub timeout_ms: Option<u64>,
74}
75
76/// Forward response payload returned by the service bus.
77#[derive(Debug, Serialize, Deserialize, Clone)]
78pub struct ServiceBusForwardResponse {
79    /// Response message type.
80    #[serde(rename = "messageType")]
81    pub message_type: String,
82    /// Free-form response payload.
83    #[serde(default)]
84    pub payload: HashMap<String, serde_json::Value>,
85}
86
87/// Generic wire frame used by all bus protocol message types.
88#[derive(Debug, Serialize, Deserialize, Clone, Default)]
89pub struct ServiceBusProtocolMessage {
90    /// Frame type; see `protocol_types` constants.
91    pub r#type: String,
92    /// Service id used in connect/auth related frames.
93    #[serde(rename = "serviceId")]
94    pub service_id: Option<String>,
95    /// Instance id used in connect/auth related frames.
96    #[serde(rename = "instanceId")]
97    pub instance_id: Option<String>,
98    /// Authentication token used during connect.
99    pub token: Option<String>,
100    /// Topic list for subscribe/unsubscribe frames.
101    pub topics: Option<Vec<String>>,
102    /// Event payload for publish/event frames.
103    pub event: Option<ServiceBusEventEnvelope>,
104    /// Forward request payload.
105    #[serde(rename = "forwardRequest")]
106    pub forward_request: Option<ServiceBusForwardRequest>,
107    /// Forward response payload.
108    #[serde(rename = "forwardResponse")]
109    pub forward_response: Option<ServiceBusForwardResponse>,
110    /// Human-readable status or error message.
111    pub message: Option<String>,
112    /// Structured protocol error code.
113    #[serde(rename = "errorCode")]
114    pub error_code: Option<String>,
115    /// Number of subscribers that received a published event.
116    #[serde(rename = "subscriberCount")]
117    pub subscriber_count: Option<i32>,
118}