bbx_net/
error.rs

1//! Error types for bbx_net network operations.
2
3use std::fmt;
4
5/// Error codes for bbx_net network operations.
6///
7/// Uses `#[repr(C)]` for C-compatible memory layout, enabling FFI usage.
8#[repr(C)]
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub enum NetError {
11    /// Invalid OSC or WebSocket address format.
12    InvalidAddress = 0,
13    /// Invalid room code provided.
14    InvalidRoomCode = 1,
15    /// Room has reached maximum client capacity.
16    RoomFull = 2,
17    /// Network connection failed.
18    ConnectionFailed = 3,
19    /// Failed to parse message.
20    ParseError = 4,
21    /// I/O error during network operation.
22    IoError = 5,
23    /// WebSocket protocol error.
24    WebSocketError = 6,
25    /// Connection or operation timeout.
26    Timeout = 7,
27    /// Invalid node ID format.
28    InvalidNodeId = 8,
29}
30
31impl fmt::Display for NetError {
32    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
33        match self {
34            NetError::InvalidAddress => write!(f, "invalid address format"),
35            NetError::InvalidRoomCode => write!(f, "invalid room code"),
36            NetError::RoomFull => write!(f, "room is at capacity"),
37            NetError::ConnectionFailed => write!(f, "connection failed"),
38            NetError::ParseError => write!(f, "message parse error"),
39            NetError::IoError => write!(f, "I/O error"),
40            NetError::WebSocketError => write!(f, "WebSocket error"),
41            NetError::Timeout => write!(f, "connection timeout"),
42            NetError::InvalidNodeId => write!(f, "invalid node ID"),
43        }
44    }
45}
46
47impl std::error::Error for NetError {}
48
49impl From<std::io::Error> for NetError {
50    fn from(_: std::io::Error) -> Self {
51        NetError::IoError
52    }
53}
54
55/// Result type alias for bbx_net operations.
56pub type Result<T> = std::result::Result<T, NetError>;