bothan_kraken/api/types/message.rs
1//! Types for Kraken WebSocket public messages.
2//!
3//! This module provides types for constructing and parsing public messages used with the Kraken WebSocket API,
4//! including subscription commands and their responses.
5//!
6//! # Key Types
7//!
8//! - [`Method`] – Enum representing available message methods.
9//! - [`PublicMessage<T>`] – Generic structure representing WebSocket requests.
10//! - [`PublicMessageResponse`] – Structure representing WebSocket responses from Kraken.
11
12use serde::{Deserialize, Serialize};
13
14/// Represents available methods for public messages to the Kraken WebSocket API.
15///
16/// This enum defines operations such as subscribing and unsubscribing from Kraken data channels.
17#[derive(Clone, Debug, Serialize, Deserialize)]
18#[serde(rename_all = "snake_case")]
19pub enum Method {
20 /// Sends a ping to keep the WebSocket connection alive.
21 Ping,
22
23 /// Subscribes to a channel to receive data updates.
24 Subscribe,
25
26 /// Unsubscribes from a channel to stop receiving data updates.
27 Unsubscribe,
28}
29
30/// Represents a public request message to the Kraken WebSocket API.
31///
32/// This generic struct defines the format for sending subscription-related requests.
33/// It includes the method, optional parameters, and a request ID.
34#[derive(Clone, Debug, Serialize, Deserialize)]
35pub struct PublicMessage<T> {
36 /// The method of the request (e.g., subscribe, unsubscribe, ping).
37 pub method: Method,
38
39 /// Optional parameters specific to the method.
40 #[serde(skip_serializing_if = "Option::is_none")]
41 pub params: Option<T>,
42
43 /// Optional request identifier for correlating responses.
44 #[serde(skip_serializing_if = "Option::is_none")]
45 pub req_id: Option<usize>,
46}
47
48/// Represents a response message received from the Kraken WebSocket API.
49///
50/// This struct contains metadata indicating the result of public WebSocket requests,
51/// including subscription status and any associated errors.
52#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
53pub struct PublicMessageResponse {
54 /// Error message
55 pub error: Option<String>,
56
57 /// Method associated with the response (e.g., subscribe, unsubscribe).
58 pub method: String,
59
60 /// Optional client originated request identifier sent as acknowledgment in the response.
61 pub req_id: Option<usize>,
62
63 /// Indicates whether the operation succeeded.
64 pub success: bool,
65
66 /// Timestamp when the subscription was received on the wire, immediately before data parsing (RFC3339 format, e.g., `2022-12-25T09:30:59.123456Z`).
67 pub time_in: String,
68
69 /// Timestamp when the acknowledgment was sent on the wire, immediately before data transmission (RFC3339 format, e.g., `2022-12-25T09:30:59.123456Z`).
70 pub time_out: String,
71}