1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
use std::fmt;
use std::io;
#[derive(Debug)]
pub enum Error {
Io(io::Error),
/// Bad magic number in header.
BadMagic(u16),
/// Protocol version not supported.
UnsupportedVersion(u8),
/// Unknown message type byte.
UnknownMessageType(u8),
/// Packet or buffer too small for the operation.
BufferTooSmall,
/// Payload length doesn't match declared length.
PayloadMismatch,
/// Generic invalid message (malformed content).
InvalidMessage,
/// Not connected to the network.
NotConnected,
/// Operation timed out.
Timeout,
/// Signature verification failed.
BadSignature,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Io(e) => write!(f, "I/O error: {e}"),
Error::BadMagic(m) => {
write!(f, "bad magic: 0x{m:04x}")
}
Error::UnsupportedVersion(v) => {
write!(f, "unsupported version: {v}")
}
Error::UnknownMessageType(t) => {
write!(f, "unknown message type: 0x{t:02x}")
}
Error::BufferTooSmall => {
write!(f, "buffer too small")
}
Error::PayloadMismatch => {
write!(f, "payload length mismatch")
}
Error::InvalidMessage => {
write!(f, "invalid message")
}
Error::NotConnected => write!(f, "not connected"),
Error::Timeout => write!(f, "timeout"),
Error::BadSignature => {
write!(f, "signature verification failed")
}
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Self {
Error::Io(e)
}
}
|