Skip to main content

vibe_graph_api/
types.rs

1//! API types and DTOs.
2
3use serde::{Deserialize, Serialize};
4use std::sync::Arc;
5use std::time::{SystemTime, UNIX_EPOCH};
6use tokio::sync::{broadcast, RwLock};
7use vibe_graph_core::{GitChangeSnapshot, SourceCodeGraph};
8
9/// Shared application state for the API.
10pub struct ApiState {
11    /// The source code graph.
12    pub graph: Arc<RwLock<SourceCodeGraph>>,
13    /// Current git change snapshot.
14    pub git_changes: Arc<RwLock<GitChangeSnapshot>>,
15    /// Broadcast channel for WebSocket messages.
16    pub tx: broadcast::Sender<WsServerMessage>,
17}
18
19/// Response wrapper with timestamp.
20#[derive(Debug, Clone, Serialize, Deserialize)]
21pub struct ApiResponse<T> {
22    /// Response data.
23    pub data: T,
24    /// Unix timestamp in milliseconds.
25    pub timestamp: u64,
26}
27
28impl<T> ApiResponse<T> {
29    /// Create a new API response with current timestamp.
30    pub fn new(data: T) -> Self {
31        let timestamp = SystemTime::now()
32            .duration_since(UNIX_EPOCH)
33            .map(|d| d.as_millis() as u64)
34            .unwrap_or(0);
35        Self { data, timestamp }
36    }
37}
38
39/// Health check response.
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct HealthResponse {
42    /// Service status.
43    pub status: String,
44    /// Number of nodes in the graph.
45    pub nodes: usize,
46    /// Number of edges in the graph.
47    pub edges: usize,
48}
49
50/// WebSocket message sent from server to client.
51#[derive(Debug, Clone, Serialize, Deserialize)]
52#[serde(tag = "type", rename_all = "snake_case")]
53pub enum WsServerMessage {
54    /// Git changes detected.
55    GitChanges {
56        /// The git change snapshot.
57        data: GitChangeSnapshot,
58    },
59    /// Graph was updated.
60    GraphUpdated {
61        /// New node count.
62        node_count: usize,
63        /// New edge count.
64        edge_count: usize,
65    },
66    /// Error occurred.
67    Error {
68        /// Error code.
69        code: String,
70        /// Error message.
71        message: String,
72    },
73    /// Pong response to client ping.
74    Pong,
75}
76
77/// WebSocket message sent from client to server.
78#[derive(Debug, Clone, Serialize, Deserialize)]
79#[serde(tag = "type", rename_all = "snake_case")]
80pub enum WsClientMessage {
81    /// Subscribe to topics.
82    Subscribe {
83        /// Topics to subscribe to.
84        topics: Vec<String>,
85    },
86    /// Ping to keep connection alive.
87    Ping,
88}