1use std::fmt;
2
3use crate::packet::SerializationError;
4
5#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum DisconnectReason {
8 Transport,
10 DisconnectedByClient,
12 DisconnectedByServer,
14 PacketSerialization(SerializationError),
16 PacketDeserialization(SerializationError),
18 ReceivedInvalidChannelId(u8),
20 SendChannelError { channel_id: u8, error: ChannelError },
22 ReceiveChannelError { channel_id: u8, error: ChannelError },
24}
25
26impl std::error::Error for DisconnectReason {}
27
28#[derive(Clone, Copy, Debug, PartialEq, Eq)]
30pub enum ChannelError {
31 ReliableChannelMaxMemoryReached,
33 InvalidSliceMessage,
35}
36
37impl fmt::Display for ChannelError {
38 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
39 use ChannelError::*;
40
41 match *self {
42 ReliableChannelMaxMemoryReached => write!(fmt, "reliable channel memory usage was exhausted"),
43 InvalidSliceMessage => write!(fmt, "received an invalid slice packet"),
44 }
45 }
46}
47
48impl fmt::Display for DisconnectReason {
49 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
50 use DisconnectReason::*;
51
52 match *self {
53 Transport => write!(fmt, "connection terminated by the transport layer"),
54 DisconnectedByClient => write!(fmt, "connection terminated by the client"),
55 DisconnectedByServer => write!(fmt, "connection terminated by the server"),
56 PacketSerialization(err) => write!(fmt, "failed to serialize packet: {err}"),
57 PacketDeserialization(err) => write!(fmt, "failed to deserialize packet: {err}"),
58 ReceivedInvalidChannelId(id) => write!(fmt, "received message with invalid channel {id}"),
59 SendChannelError { channel_id, error } => write!(fmt, "send channel {channel_id} with error: {error}"),
60 ReceiveChannelError { channel_id, error } => write!(fmt, "receive channel {channel_id} with error: {error}"),
61 }
62 }
63}
64
65impl std::error::Error for ChannelError {}
66
67#[derive(Debug)]
68pub struct ClientNotFound;
69
70impl std::error::Error for ClientNotFound {}
71
72impl fmt::Display for ClientNotFound {
73 fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
74 write!(fmt, "client with given id was not found")
75 }
76}