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