veloren_network_protocol/
error.rs1#[derive(Debug, PartialEq, Eq)]
5pub enum InitProtocolError<E: std::fmt::Debug + Send> {
6 Custom(E),
7 NotHandshake,
9 NotId,
11 WrongMagicNumber([u8; 7]),
12 WrongVersion([u32; 3]),
13}
14
15#[derive(Debug, PartialEq, Eq)]
17pub enum ProtocolError<E: std::fmt::Debug + Send> {
18 Custom(E),
21 Violated,
25}
26
27impl<E: std::fmt::Debug + Send> From<ProtocolError<E>> for InitProtocolError<E> {
28 fn from(err: ProtocolError<E>) -> Self {
29 match err {
30 ProtocolError::Custom(e) => InitProtocolError::Custom(e),
31 ProtocolError::Violated => {
32 unreachable!("not possible as the Init has raw access to the I/O")
33 },
34 }
35 }
36}
37
38impl<E: std::fmt::Debug + Send> core::fmt::Display for InitProtocolError<E> {
39 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
40 match self {
41 InitProtocolError::Custom(e) => write!(f, "custom: {:?}", e),
42 InitProtocolError::NotHandshake => write!(
43 f,
44 "Remote send something which couldn't be parsed as a handshake"
45 ),
46 InitProtocolError::NotId => {
47 write!(f, "Remote send something which couldn't be parsed as an id")
48 },
49 InitProtocolError::WrongMagicNumber(r) => write!(
50 f,
51 "Magic Number doesn't match, remote side send '{:?}' instead of '{:?}'",
52 &r,
53 &crate::types::VELOREN_MAGIC_NUMBER
54 ),
55 InitProtocolError::WrongVersion(r) => write!(
56 f,
57 "Network doesn't match, remote side send '{:?}' we are on '{:?}'",
58 &r,
59 &crate::types::VELOREN_NETWORK_VERSION
60 ),
61 }
62 }
63}
64
65impl<E: std::fmt::Debug + Send> core::fmt::Display for ProtocolError<E> {
66 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
67 match self {
68 ProtocolError::Custom(e) => write!(f, "Channel custom close: {:?}", e),
69 ProtocolError::Violated => write!(f, "Channel protocol violated"),
70 }
71 }
72}
73
74impl<E: std::fmt::Debug + Send> std::error::Error for InitProtocolError<E> {}
75impl<E: std::fmt::Debug + Send> std::error::Error for ProtocolError<E> {}