Skip to main content

csi_webclient/core/
messages.rs

1use serde_json::Value;
2
3/// Commands sent from the app orchestration layer to the core worker.
4#[derive(Debug, Clone)]
5pub enum CoreCommand {
6    /// Execute one HTTP request against the configured webserver.
7    ExecuteApi(ApiRequest),
8    /// Open or replace the active WebSocket stream connection.
9    ConnectWebSocket { url: String },
10    /// Stop the active WebSocket stream connection, if any.
11    DisconnectWebSocket,
12    /// Stop the core worker gracefully.
13    Shutdown,
14}
15
16/// A normalized HTTP request model consumed by the core worker.
17#[derive(Debug, Clone)]
18pub struct ApiRequest {
19    /// Logical operation label used for UI messages and event correlation.
20    pub label: String,
21    /// HTTP verb to send.
22    pub method: HttpMethod,
23    /// Base URL (for example, `http://127.0.0.1:3000`).
24    pub base_url: String,
25    /// Request path (for example, `/api/config/wifi`).
26    pub path: String,
27    /// Optional JSON body.
28    pub body: Option<Value>,
29}
30
31/// Event payload returned to the app after an HTTP request.
32#[derive(Debug, Clone)]
33pub struct ApiResponseEvent {
34    /// Logical request label echoed from [`ApiRequest::label`].
35    pub label: String,
36    /// True when status code is in the 2xx range.
37    pub success: bool,
38    /// HTTP status code. `0` is used for transport-level failures.
39    pub status: u16,
40    /// Human-readable message for status/error UI.
41    pub message: String,
42    /// Parsed response payload, if available.
43    pub data: Option<Value>,
44}
45
46/// Events emitted by the core worker and consumed by the app layer.
47#[derive(Debug, Clone)]
48pub enum CoreEvent {
49    /// HTTP request completed.
50    ApiResponse(ApiResponseEvent),
51    /// WebSocket connection successfully established.
52    WebSocketConnected,
53    /// WebSocket connection ended.
54    WebSocketDisconnected { reason: String },
55    /// One WebSocket payload received from server.
56    WebSocketFrame(Vec<u8>),
57    /// Internal diagnostic log line from core worker/runtime.
58    Log(String),
59}
60
61/// Supported HTTP methods in this client.
62#[derive(Debug, Clone)]
63pub enum HttpMethod {
64    /// `GET`.
65    Get,
66    /// `POST`.
67    Post,
68}