From c7456f0a67b5488cca777ea702144cd9996698c0 Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 10 May 2024 20:08:56 +0200 Subject: [PATCH] From> for Packet --- src/packet.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/packet.rs b/src/packet.rs index 3b6655f..a3ba5f2 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -19,4 +19,24 @@ impl Into> for Packet { return packet; } +} + +fn u16_from_be_slice(slice: &[u8]) -> u16 { + let mut bytes = [0u8; 2]; + bytes[0] = slice[0]; + bytes[1] = slice[1]; + u16::from_be_bytes(bytes) +} + +impl From> for Packet { + fn from(value: Vec) -> Self { + let mode = u16_from_be_slice(&value[0..=1]); + let a = u16_from_be_slice(&value[2..=3]); + let b = u16_from_be_slice(&value[4..=5]); + let c = u16_from_be_slice(&value[6..=7]); + let d = u16_from_be_slice(&value[8..=9]); + let payload = value[10..].to_vec(); + + Packet(Header(mode, a, b, c, d), payload) + } } \ No newline at end of file