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

mod bytes;
pub use self::bytes::*;

mod body_bytes;
pub use body_bytes::*;

mod packet;
pub use packet::*;

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

pub(crate) mod builder;

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

use std::fmt;


#[derive(Debug)]
pub enum PacketError {
	Header(String),
	Body(String),
	#[cfg(feature = "json")]
	Json(serde_json::Error),
	#[cfg(feature = "fs")]
	Io(std::io::Error),
	/// Returns the size that should have been sent
	BodyLimitReached(usize)
}

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 = "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)
	}
}