nardol/packet/packet_kind.rs
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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
use serde::{Deserialize, Serialize};
use crate::bytes::Bytes;
use crate::error::{Error, ErrorKind};
use crate::ron::{FromRon, ToRon};
/// Determines kind of [Packet](super::Packet).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum PacketKind {
/// Empty packet usually used only for testing or like a placeholder.
Empty,
/// Part of `metadata`.
MetaData,
/// Same as [MetaData](PacketKind::MetaData), only marking end of it.
MetaDataEnd,
/// Content packet.
Content,
/// Used to signalize end of the [`message`].
///
/// Can be [TcpMessage](crate::message::TcpMessage)
/// or an end in general (of all times and you know it).
End,
/// Usually used in udp context for signaling that given [Packet](super::Packet)
/// is the whole `message`.
Unit,
/// Used in case if [PacketKind] is not recognized.
Unknown,
}
impl ToRon for PacketKind {}
impl FromRon<'_> for PacketKind {}
impl TryFrom<Bytes> for PacketKind {
type Error = Error;
fn try_from(value: Bytes) -> Result<Self, Self::Error> {
// Checks if value has enough bytes to parse to PacketKind (at least 2).
match value.get(1) {
Some(_) => &value[0..2],
None => {
return Err(Error::new(
ErrorKind::InvalidBufferSize,
Some(
concat!(
"impl TryFrom<Bytes> for PacketKind requires buffer of length",
"of at least 2 bytes.",
)
.to_string(),
),
))
}
};
let kind = match value[0] {
0 => PacketKind::Empty,
1 => match value[1] {
0 => PacketKind::MetaData,
1 => PacketKind::MetaDataEnd,
_ => PacketKind::Unknown,
},
2 => PacketKind::Content,
3 => PacketKind::End,
4 => PacketKind::Unit,
_ => PacketKind::Unknown,
};
Ok(kind)
}
}
impl TryFrom<&[u8]> for PacketKind {
type Error = Error;
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
// Checks if value has enough bytes to parse to PacketKind (at least 2).
match value.get(1) {
Some(_) => &value[0..2],
None => {
return Err(Error::new(
ErrorKind::InvalidBufferSize,
Some(
concat!(
"impl TryFrom<&[u8]> for PacketKind requires buffer of length",
"of at least 2 bytes.",
)
.to_string(),
),
))
}
};
let kind = match value[0] {
0 => PacketKind::Empty,
1 => match value[1] {
0 => PacketKind::MetaData,
1 => PacketKind::MetaDataEnd,
_ => PacketKind::Unknown,
},
2 => PacketKind::Content,
3 => PacketKind::End,
4 => PacketKind::Unit,
_ => PacketKind::Unknown,
};
Ok(kind)
}
}
impl From<PacketKind> for Bytes {
fn from(value: PacketKind) -> Self {
let mut bytes = Bytes::empty();
match value {
PacketKind::Empty => bytes.append(&mut Bytes::from([0, 0])),
PacketKind::MetaData => bytes.append(&mut Bytes::from([1, 0])),
PacketKind::MetaDataEnd => bytes.append(&mut Bytes::from([1, 1])),
PacketKind::Content => bytes.append(&mut Bytes::from([2, 0])),
PacketKind::End => bytes.append(&mut Bytes::from([3, 0])),
PacketKind::Unit => bytes.append(&mut Bytes::from([4, 0])),
PacketKind::Unknown => bytes.append(&mut Bytes::from([255, 0])),
}
bytes
}
}