From fc5cbdb36148001eeb11604734a358987897c4db Mon Sep 17 00:00:00 2001 From: Raine Date: Sun, 15 Oct 2023 18:06:23 +0200 Subject: [PATCH] feat: add packet_id() to Packet enum --- domo_proto/src/packet/identifier.rs | 14 ++++++++++++-- domo_proto/src/packet/mod.rs | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/domo_proto/src/packet/identifier.rs b/domo_proto/src/packet/identifier.rs index da12e61..a89da1c 100644 --- a/domo_proto/src/packet/identifier.rs +++ b/domo_proto/src/packet/identifier.rs @@ -1,10 +1,20 @@ -use std::fmt::{Formatter, LowerHex}; +use std::fmt::{Display, Formatter, LowerHex}; -#[derive(Debug, Eq, PartialEq)] +#[derive(Debug, Eq, PartialEq, Copy, Clone)] pub struct Identifier { pub bytes: [u8; 4], } +impl Display for Identifier { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!( + f, + "0x{:x}{:x}{:x}{:x}", + self.bytes[0], self.bytes[1], self.bytes[2], self.bytes[3] + ) + } +} + impl LowerHex for Identifier { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!( diff --git a/domo_proto/src/packet/mod.rs b/domo_proto/src/packet/mod.rs index 66a6796..8160c05 100644 --- a/domo_proto/src/packet/mod.rs +++ b/domo_proto/src/packet/mod.rs @@ -21,6 +21,12 @@ pub enum Packet { } impl Packet { + pub fn packet_id(&self) -> Identifier { + match self { + Packet::V1 { packet_id, ..} => packet_id.clone() + } + } + /// Build packet **without** CRC32 pub fn build_base_packet(&self) -> Vec { match self {