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
mod bytes;
pub use self::bytes::*;

mod body_bytes;
pub use body_bytes::*;

mod packet;
pub use packet::*;

#[cfg(test)]
pub mod test;

#[cfg(feature = "encrypted")]
mod encrypted_bytes;
#[cfg(feature = "encrypted")]
#[cfg_attr(docsrs, doc(cfg(feature = "encrypted")))]
pub use encrypted_bytes::*;

#[cfg(feature = "connection")]
pub(crate) mod builder;

pub type Result<T> = std::result::Result<T, PacketError>;

use std::fmt;
use std::borrow::Cow;


#[derive(Debug)]
#[non_exhaustive]
pub enum PacketError {
	Header(Cow<'static, str>),
	Body(Cow<'static, str>),
	#[cfg(feature = "json")]
	#[cfg_attr(docsrs, doc(cfg(feature = "json")))]
	Json(serde_json::Error),
	#[cfg(feature = "fs")]
	#[cfg_attr(docsrs, doc(cfg(feature = "fs")))]
	Io(std::io::Error),
	/// Returns the size that should have been sent
	BodyLimitReached(u32),
	#[cfg(feature = "encrypted")]
	#[cfg_attr(docsrs, doc(cfg(feature = "encrypted")))]
	MacNotEqual
}

impl fmt::Display for PacketError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			Self::Header(s) => write!(f, "PacketError::Header: {}", s),
			Self::Body(s) => write!(f, "PacketError::Body: {}", s),
			#[cfg(feature = "json")]
			Self::Json(s) => write!(f, "PacketError::Json: {}", s),
			#[cfg(feature = "fs")]
			Self::Io(s) => write!(f, "PacketError::Io: {}", s),
			Self::BodyLimitReached(s) => {
				write!(f, "PacketError::BodyLimitReached: {}", s)
			},
			#[cfg(feature = "encrypted")]
			Self::MacNotEqual => write!(f, "PacketError::MacNotEqual")
		}
	}
}

#[cfg(feature = "json")]
impl From<serde_json::Error> for PacketError {
	fn from(e: serde_json::Error) -> Self {
		Self::Json(e)
	}
}

#[cfg(feature = "fs")]
impl From<std::io::Error> for PacketError {
	fn from(e: std::io::Error) -> Self {
		Self::Io(e)
	}
}