use serde::{Deserialize, Serialize};
use std::io;
use thiserror::Error;
pub mod constants {
pub const ERR_DISPATCHER_WRITE_LOCK: &str = "Failed to acquire write lock on dispatcher";
pub const ERR_DISPATCHER_READ_LOCK: &str = "Failed to acquire read lock on dispatcher";
pub const ERR_INVALID_HEADER: &str = "Invalid protocol header";
pub const ERR_INVALID_PACKET: &str = "Invalid packet structure";
pub const ERR_OVERSIZED_PACKET: &str = "Packet exceeds maximum size";
pub const ERR_CONNECTION_CLOSED: &str = "Connection closed";
pub const ERR_CONNECTION_TIMEOUT: &str = "Connection timed out (no activity)";
pub const ERR_TIMEOUT: &str = "Operation timed out";
pub const ERR_ENCRYPTION_FAILED: &str = "Encryption failed";
pub const ERR_DECRYPTION_FAILED: &str = "Decryption failed";
pub const ERR_COMPRESSION_FAILED: &str = "Compression failed";
pub const ERR_DECOMPRESSION_FAILED: &str = "Decompression failed";
pub const ERR_UNSUPPORTED_VERSION: &str = "Unsupported protocol version";
pub const ERR_HANDSHAKE_FAILED: &str = "Handshake failed";
pub const ERR_UNEXPECTED_MESSAGE: &str = "Unexpected message type";
pub const ERR_SECURITY_ERROR: &str = "Security violation detected";
pub const ERR_LOCK_POISONED: &str = "Synchronization primitive poisoned";
pub const ERR_SYSTEM_TIME: &str = "System time error: time went backwards";
pub const ERR_INVALID_TIMESTAMP: &str = "Invalid or stale timestamp";
pub const ERR_REPLAY_ATTACK: &str = "Replay attack detected - nonce/timestamp already seen";
pub const ERR_CLIENT_NONCE_NOT_FOUND: &str = "Client nonce not found";
pub const ERR_SERVER_NONCE_NOT_FOUND: &str = "Server nonce not found";
pub const ERR_CLIENT_SECRET_NOT_FOUND: &str = "Client secret not found";
pub const ERR_SERVER_SECRET_NOT_FOUND: &str = "Server secret not found";
pub const ERR_CLIENT_PUBLIC_NOT_FOUND: &str = "Client public key not found";
pub const ERR_SERVER_PUBLIC_NOT_FOUND: &str = "Server public key not found";
pub const ERR_NONCE_VERIFICATION_FAILED: &str = "Server failed to verify client nonce";
pub const ERR_SERVER_VERIFICATION_FAILED: &str = "Client failed to verify server nonce";
}
#[derive(Error, Debug, Serialize, Deserialize)]
pub enum ProtocolError {
#[error("I/O error: {0}")]
#[serde(skip_serializing, skip_deserializing)]
Io(#[from] io::Error),
#[error("Serialization error: {0}")]
#[serde(skip_serializing, skip_deserializing)]
Serialization(#[from] bincode::Error),
#[error("Serialize error: {0}")]
SerializeError(String),
#[error("Deserialize error: {0}")]
DeserializeError(String),
#[error("Serialization error: {0}")]
SerializationError(String),
#[error("Transport error: {0}")]
TransportError(String),
#[error("Connection closed")]
ConnectionClosed,
#[error("Security error: {0}")]
SecurityError(String),
#[error("Invalid protocol header")]
InvalidHeader,
#[error("Unsupported protocol version: {0}")]
UnsupportedVersion(u8),
#[error("Packet too large: {0} bytes")]
OversizedPacket(usize),
#[error("Decryption failed")]
DecryptionFailure,
#[error("Encryption failed")]
EncryptionFailure,
#[error("Compression failed")]
CompressionFailure,
#[error("Decompression failed")]
DecompressionFailure,
#[error("Handshake failed: {0}")]
HandshakeError(String),
#[error("Unexpected message type")]
UnexpectedMessage,
#[error("Timeout occurred")]
Timeout,
#[error("Connection timed out (no activity)")]
ConnectionTimeout,
#[error("Configuration error: {0}")]
ConfigError(String),
#[error("Circuit breaker open - too many consecutive failures")]
CircuitBreakerOpen,
#[error("Connection pool exhausted - backpressure limit reached")]
PoolExhausted,
#[error("Custom error: {0}")]
Custom(String),
#[error("TLS error: {0}")]
TlsError(String),
}
pub type Result<T> = std::result::Result<T, ProtocolError>;