proto_packet/io/decode/
decoding_error.rs1use crate::io::WireType;
2use std::fmt::{Display, Formatter};
3use std::string::FromUtf8Error;
4
5#[derive(Debug)]
7pub enum DecodingError {
8 Stream(std::io::Error),
10
11 InvalidWireType {
13 semantic: &'static str,
14 wire: WireType,
15 },
16
17 ValueOutOfRange,
19
20 LengthPrefixOutOfRange,
22
23 InvalidPacket(enc::Error),
25
26 PacketUnderRead { declared: usize, unread: usize },
32
33 InvalidBool(u8),
35
36 InvalidString(FromUtf8Error),
38}
39
40impl DecodingError {
41 pub fn from_var_int_error(error: enc::Error) -> Self {
45 match error {
46 enc::Error::Stream(error) => Self::Stream(error),
47 _ => Self::ValueOutOfRange,
48 }
49 }
50
51 pub fn from_length_prefix_error(error: enc::Error) -> Self {
53 match error {
54 enc::Error::Stream(error) => Self::Stream(error),
55 _ => Self::LengthPrefixOutOfRange,
56 }
57 }
58
59 pub fn from_packet_error(error: enc::Error) -> Self {
61 match error {
62 enc::Error::Stream(error) => Self::Stream(error),
63 _ => Self::InvalidPacket(error),
64 }
65 }
66}
67
68impl From<std::io::Error> for DecodingError {
69 fn from(error: std::io::Error) -> Self {
70 Self::Stream(error)
71 }
72}
73
74impl From<DecodingError> for enc::Error {
75 fn from(error: DecodingError) -> Self {
76 match error {
77 DecodingError::Stream(error) => error.into(),
78 _ => enc::Error::InvalidEncodedData {
79 reason: Some(Box::new(error)),
80 },
81 }
82 }
83}
84
85impl Display for DecodingError {
86 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
87 write!(f, "{:?}", self)
88 }
89}
90
91impl std::error::Error for DecodingError {
92 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
93 match self {
94 Self::Stream(error) => Some(error),
95 Self::InvalidPacket(error) => Some(error),
96 Self::InvalidString(error) => Some(error),
97 Self::InvalidWireType { .. }
98 | Self::ValueOutOfRange
99 | Self::LengthPrefixOutOfRange
100 | Self::PacketUnderRead { .. }
101 | Self::InvalidBool(_) => None,
102 }
103 }
104}