turboclaude_protocol/
error.rs

1//! Error types for protocol operations
2//!
3//! Provides error types for serialization, deserialization, and protocol validation.
4
5use std::fmt;
6
7/// Result type for protocol operations
8pub type Result<T> = std::result::Result<T, ProtocolError>;
9
10/// Errors that can occur during protocol operations
11#[derive(Debug, Clone)]
12pub enum ProtocolError {
13    /// JSON serialization/deserialization error
14    SerializationError(String),
15
16    /// Invalid message format
17    InvalidMessage(String),
18
19    /// Missing required field
20    MissingField(String),
21
22    /// Invalid content block type
23    InvalidContentBlock(String),
24
25    /// Protocol version mismatch
26    VersionMismatch {
27        /// The expected protocol version.
28        expected: u32,
29        /// The version that was actually received.
30        got: u32,
31    },
32
33    /// Invalid control request
34    InvalidControlRequest(String),
35
36    /// Permission denied
37    PermissionDenied(String),
38
39    /// Generic protocol error
40    Other(String),
41}
42
43impl fmt::Display for ProtocolError {
44    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45        match self {
46            Self::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
47            Self::InvalidMessage(msg) => write!(f, "Invalid message: {}", msg),
48            Self::MissingField(field) => write!(f, "Missing required field: {}", field),
49            Self::InvalidContentBlock(msg) => write!(f, "Invalid content block: {}", msg),
50            Self::VersionMismatch { expected, got } => {
51                write!(f, "Version mismatch: expected {}, got {}", expected, got)
52            }
53            Self::InvalidControlRequest(msg) => write!(f, "Invalid control request: {}", msg),
54            Self::PermissionDenied(msg) => write!(f, "Permission denied: {}", msg),
55            Self::Other(msg) => write!(f, "{}", msg),
56        }
57    }
58}
59
60impl std::error::Error for ProtocolError {}
61
62impl From<serde_json::Error> for ProtocolError {
63    fn from(err: serde_json::Error) -> Self {
64        Self::SerializationError(err.to_string())
65    }
66}