Skip to main content

wscall_client/
client_types.rs

1use std::time::Duration;
2
3use serde_json::Value;
4use thiserror::Error;
5use wscall_protocol::{ErrorPayload, FileAttachment, PacketEnvelope, ProtocolError};
6
7pub(crate) enum ClientOutbound {
8    Packet(PacketEnvelope),
9    Ping(Vec<u8>),
10    Pong(Vec<u8>),
11    Close,
12}
13
14/// Lifecycle payload emitted when the client establishes a websocket session.
15#[derive(Clone, Debug)]
16pub struct ClientConnectionEvent {
17    /// Connected websocket URL.
18    pub url: String,
19}
20
21/// Lifecycle payload emitted when the client loses its websocket session.
22#[derive(Clone, Debug)]
23pub struct ClientDisconnectEvent {
24    /// Disconnected websocket URL.
25    pub url: String,
26    /// Human-readable disconnect reason.
27    pub reason: String,
28    /// Whether the client will keep trying to reconnect.
29    pub will_reconnect: bool,
30    /// Delay before the next reconnect attempt when reconnect is enabled.
31    pub retry_after: Option<Duration>,
32}
33
34/// Server-originated event delivered to a registered client event handler.
35#[derive(Clone)]
36pub struct EventMessage {
37    /// Event correlation id.
38    pub event_id: String,
39    /// Event name.
40    pub name: String,
41    /// Raw JSON event data.
42    pub data: Value,
43    /// Attachments sent with the event.
44    pub attachments: Vec<FileAttachment>,
45    /// Raw metadata payload.
46    pub metadata: Value,
47}
48
49/// Errors produced by the reusable WSCALL client.
50#[derive(Debug, Error)]
51pub enum ClientError {
52    #[error("websocket error: {0}")]
53    WebSocket(#[from] tokio_tungstenite::tungstenite::Error),
54    #[error("protocol error: {0}")]
55    Protocol(#[from] ProtocolError),
56    #[error("client disconnected")]
57    Disconnected,
58    #[error("connection closed: {0}")]
59    ConnectionClosed(String),
60    #[error("connection idle timeout")]
61    IdleTimeout,
62    #[error("request timed out")]
63    Timeout,
64    #[error("remote error: {0:?}")]
65    Remote(ErrorPayload),
66}