error.rs
1 use super::wire::DisconnectReason; 2 3 use embedded_io_async::ReadExactError; 4 5 /// Set of possible protocol errors. 6 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 7 pub enum ProtocolError { 8 /// The packet buffer was too small to hold a packet. 9 BufferExhausted, 10 /// The client sent an invalid identification string. 11 BadIdentificationString, 12 /// A string was not supplied in the required encoding. 13 BadStringEncoding, 14 /// A name list was found to contain an invalid name. 15 BadNameList, 16 /// A string had a length different than was expected. 17 BadStringLength, 18 /// There were some bytes left after object decoding. 19 TrailingPayload, 20 /// A malformed client packet was seen by the server. 21 MalformedPacket, 22 /// The channel's window overflowed during transfer. 23 WindowOverflow, 24 } 25 26 impl From<core::str::Utf8Error> for ProtocolError { 27 fn from(_: core::str::Utf8Error) -> Self { 28 Self::BadStringEncoding 29 } 30 } 31 32 /// Set of possible transport errors. 33 #[derive(Clone, Copy, Debug, Eq, PartialEq)] 34 pub enum Error<E> { 35 /// The transport's stream reached EOF unexpectedly. 36 UnexpectedEof, 37 /// The transport's stream encountered an I/O error. 38 IO(E), 39 /// The transport encountered a fatal protocol error. 40 Protocol(ProtocolError), 41 /// The server ended communication by disconnecting. 42 ServerDisconnect(DisconnectReason), 43 /// The client ended communication by disconnecting. 44 ClientDisconnect(DisconnectReason), 45 } 46 47 impl<E: embedded_io_async::Error> From<E> for Error<E> { 48 fn from(value: E) -> Self { 49 Self::IO(value) 50 } 51 } 52 53 impl<E> From<ProtocolError> for Error<E> { 54 fn from(value: ProtocolError) -> Self { 55 Self::Protocol(value) 56 } 57 } 58 59 impl<E: embedded_io_async::Error> From<ReadExactError<E>> for Error<E> { 60 fn from(value: ReadExactError<E>) -> Self { 61 match value { 62 ReadExactError::UnexpectedEof => Self::UnexpectedEof, 63 ReadExactError::Other(other_err) => other_err.into(), 64 } 65 } 66 }