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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::error::Error;
use std::fmt::{Display, Formatter};
use std::net::AddrParseError;

use quinn::ConnectError;

use crate as durian;
use durian_macros::ErrorOnlyMessage;

/// Error types when receiving an error on one of the [`PacketManager`](`crate::PacketManager`) APIs
#[derive(Default, PartialEq, Eq, Copy, Clone, Debug)]
pub enum ErrorType {
    /// This was an unexpected error and should be treated as such
    #[default]
    Unexpected,
    /// The stream or connection was disconnected.  The [`PacketManager`](`crate::PacketManager`) would have cleaned up the connection so
    /// subsequent calls should not run into this again for the same address.  Depending on the application, this may
    /// be perfectly normal/expected, or an error.
    Disconnected,
}

/// Error when calling [`PacketManager::register_receive_packet()`](`crate::PacketManager::register_receive_packet()`), [`PacketManager::received_all()`](`crate::PacketManager::received_all()`),
/// [`PacketManager::async_received_all()`](`crate::PacketManager::async_received_all()`), [`PacketManager::received()`](`crate::PacketManager::received()`), [`PacketManager::async_received()`](`crate::PacketManager::async_received()`)
#[derive(Debug, Clone, ErrorOnlyMessage)]
pub struct ReceiveError {
    /// Error message
    pub message: String,
    /// Error type
    pub error_type: ErrorType,
}

impl Display for ReceiveError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "ReceiveError(message: {}, error_type: {:?})", self.message, self.error_type)
    }
}

/// Error when calling [`PacketManager::register_send_packet()`](`crate::PacketManager::register_send_packet()`), [`PacketManager::broadcast()`](`crate::PacketManager::broadcast()`),
/// [`PacketManager::async_broadcast()`](`crate::PacketManager::async_broadcast()`), [`PacketManager::send()`](`crate::PacketManager::send()`), [`PacketManager::async_send()`](`crate::PacketManager::async_send()`),
/// [`PacketManager::send_to()`](`crate::PacketManager::send_to()`), [`PacketManager::async_send_to()`](`crate::PacketManager::async_send_to()`)
#[derive(Debug, Clone, ErrorOnlyMessage)]
pub struct SendError {
    /// Error message
    pub message: String,
    /// Error type
    pub error_type: ErrorType,
}

impl Display for SendError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "SendError(message: {}, error_type: {:?})", self.message, self.error_type)
    }
}

/// Error when calling [`PacketManager::init_client()`](`crate::PacketManager::init_client()`), [`PacketManager::async_init_client()`](`crate::PacketManager::async_init_client()`)
/// or [`PacketManager::init_server()`](`crate::PacketManager::init_server()`), [`PacketManager::async_init_server()`](`crate::PacketManager::async_init_server()`)
#[derive(Debug, Clone, ErrorOnlyMessage)]
pub struct ConnectionError {
    /// Error message
    pub message: String,
    /// Error type
    pub error_type: ErrorType,
}

impl Display for ConnectionError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "ConnectionError(message: {}, error_type: {:?})", self.message, self.error_type)
    }
}

impl From<quinn::ConnectionError> for ConnectionError {
    fn from(e: quinn::ConnectionError) -> Self {
        ConnectionError::new(format!("ConnectionError: {:?}", e))
    }
}

impl From<Box<dyn Error>> for ConnectionError {
    fn from(e: Box<dyn Error>) -> Self {
        ConnectionError::new(format!("ConnectionError: {:?}", e))
    }
}

impl From<quinn::ConnectionError> for Box<ConnectionError> {
    fn from(e: quinn::ConnectionError) -> Self {
        Box::new(ConnectionError::new(format!("ConnectionError: {:?}", e)))
    }
}

impl From<Box<dyn Error>> for Box<ConnectionError> {
    fn from(e: Box<dyn Error>) -> Self {
        Box::new(ConnectionError::new(format!("ConnectionError: {:?}", e)))
    }
}

impl From<AddrParseError> for ConnectionError {
    fn from(e: AddrParseError) -> Self {
        ConnectionError::new(format!("ConnectionError: {:?}", e))
    }
}

impl From<ConnectError> for ConnectionError {
    fn from(e: ConnectError) -> Self {
        ConnectionError::new(format!("ConnectionError: {:?}", e))
    }
}

/// Error when calling [`PacketManager::close_connection()`](`crate::PacketManager::close_connection()`)
#[derive(Debug, Clone, ErrorOnlyMessage)]
pub struct CloseError {
    /// Error message
    pub message: String,
    /// Error type
    pub error_type: ErrorType,
}

impl Display for CloseError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "CloseError(message: {}, error_type: {:?})", self.message, self.error_type)
    }
}