mysql_connector/error/
protocol.rs1use {
2 super::{Error, ParseError},
3 crate::types::ValueType,
4 std::{convert::Infallible, fmt, io},
5};
6
7#[derive(Debug)]
8pub enum SerializeError {
9 Infallible,
10 InvalidValue(ValueType, Box<dyn fmt::Debug>),
11 #[cfg(feature = "caching-sha2-password")]
12 #[cfg_attr(doc, doc(cfg(feature = "caching-sha2-password")))]
13 Encryption(crate::utils::crypt::Error),
14}
15
16impl From<Infallible> for SerializeError {
17 fn from(_value: Infallible) -> Self {
18 Self::Infallible
19 }
20}
21
22#[cfg(feature = "caching-sha2-password")]
23#[cfg_attr(doc, doc(cfg(feature = "caching-sha2-password")))]
24impl From<crate::utils::crypt::Error> for SerializeError {
25 fn from(value: crate::utils::crypt::Error) -> Self {
26 Self::Encryption(value)
27 }
28}
29
30pub struct UnexpectedPacket(pub Vec<u8>, pub Option<&'static str>);
31
32impl fmt::Debug for UnexpectedPacket {
33 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34 if let Some(expected) = self.1 {
35 f.debug_struct("UnexpectedPacket")
36 .field("expected", &expected)
37 .field("got", &self.0)
38 .finish()
39 } else {
40 f.debug_tuple("UnexpectedPacket").field(&self.0).finish()
41 }
42 }
43}
44
45#[derive(Debug)]
46pub struct InvalidPacket {
47 pub packet: Vec<u8>,
48 pub r#type: &'static str,
49 pub error: &'static str,
50}
51
52#[derive(Debug)]
53pub enum ProtocolError {
54 Parse(ParseError),
55 Serialize(SerializeError),
56 Io(io::Error),
57 OutOfSync,
58 UnexpectedPacket(UnexpectedPacket),
59 InvalidPacket(InvalidPacket),
60 UnknownAuthPlugin(String),
61}
62
63impl ProtocolError {
64 pub fn eof() -> Self {
65 Self::Io(io::Error::new(io::ErrorKind::UnexpectedEof, "EOF"))
66 }
67
68 pub fn unexpected_packet(got: Vec<u8>, expected: Option<&'static str>) -> Self {
69 Self::UnexpectedPacket(UnexpectedPacket(got, expected))
70 }
71
72 pub fn invalid_packet(packet: Vec<u8>, r#type: &'static str, error: &'static str) -> Self {
73 Self::InvalidPacket(InvalidPacket {
74 packet,
75 r#type,
76 error,
77 })
78 }
79}
80
81impl From<ParseError> for ProtocolError {
82 fn from(value: ParseError) -> Self {
83 ProtocolError::Parse(value)
84 }
85}
86
87impl From<ParseError> for Error {
88 fn from(value: ParseError) -> Self {
89 ProtocolError::Parse(value).into()
90 }
91}
92
93impl From<SerializeError> for ProtocolError {
94 fn from(value: SerializeError) -> Self {
95 ProtocolError::Serialize(value)
96 }
97}
98
99impl From<SerializeError> for Error {
100 fn from(value: SerializeError) -> Self {
101 ProtocolError::Serialize(value).into()
102 }
103}
104
105impl From<io::Error> for ProtocolError {
106 fn from(value: io::Error) -> Self {
107 ProtocolError::Io(value)
108 }
109}
110
111impl From<io::Error> for Error {
112 fn from(value: io::Error) -> Self {
113 ProtocolError::Io(value).into()
114 }
115}