Skip to main content

vcl_protocol/
error.rs

1use std::fmt;
2
3#[derive(Debug)]
4pub enum VCLError {
5    CryptoError(String),
6    SignatureInvalid,
7    InvalidKey(String),
8    ChainValidationFailed,
9    ReplayDetected(String),
10    InvalidPacket(String),
11    ConnectionClosed,
12    Timeout,
13    NoPeerAddress,
14    NoSharedSecret,
15    HandshakeFailed(String),
16    ExpectedClientHello,
17    ExpectedServerHello,
18    SerializationError(String),
19    IoError(String),
20}
21
22impl fmt::Display for VCLError {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            VCLError::CryptoError(msg)       => write!(f, "Crypto error: {}", msg),
26            VCLError::SignatureInvalid        => write!(f, "Signature validation failed"),
27            VCLError::InvalidKey(msg)         => write!(f, "Invalid key: {}", msg),
28            VCLError::ChainValidationFailed   => write!(f, "Chain validation failed"),
29            VCLError::ReplayDetected(msg)     => write!(f, "Replay detected: {}", msg),
30            VCLError::InvalidPacket(msg)      => write!(f, "Invalid packet: {}", msg),
31            VCLError::ConnectionClosed        => write!(f, "Connection closed"),
32            VCLError::Timeout                 => write!(f, "Connection timeout"),
33            VCLError::NoPeerAddress           => write!(f, "No peer address"),
34            VCLError::NoSharedSecret          => write!(f, "No shared secret"),
35            VCLError::HandshakeFailed(msg)    => write!(f, "Handshake failed: {}", msg),
36            VCLError::ExpectedClientHello     => write!(f, "Expected ClientHello"),
37            VCLError::ExpectedServerHello     => write!(f, "Expected ServerHello"),
38            VCLError::SerializationError(msg) => write!(f, "Serialization error: {}", msg),
39            VCLError::IoError(msg)            => write!(f, "IO error: {}", msg),
40        }
41    }
42}
43
44impl std::error::Error for VCLError {}
45
46impl From<std::io::Error> for VCLError {
47    fn from(err: std::io::Error) -> Self {
48        VCLError::IoError(err.to_string())
49    }
50}
51
52impl From<bincode::Error> for VCLError {
53    fn from(err: bincode::Error) -> Self {
54        VCLError::SerializationError(err.to_string())
55    }
56}
57
58impl From<std::net::AddrParseError> for VCLError {
59    fn from(err: std::net::AddrParseError) -> Self {
60        VCLError::IoError(err.to_string())
61    }
62}
63
64impl From<VCLError> for String {
65    fn from(err: VCLError) -> Self {
66        err.to_string()
67    }
68}