From 8f6fbb4778ace51dd4eb4c152294924e3e1c2811 Mon Sep 17 00:00:00 2001 From: clerie Date: Wed, 18 Dec 2024 21:00:09 +0100 Subject: [PATCH] Convert RawPacket into ImprovPacket --- src/improv.rs | 17 +++++++++++++++++ src/main.rs | 7 ++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/improv.rs b/src/improv.rs index cd5e3e9..0cfc8a3 100644 --- a/src/improv.rs +++ b/src/improv.rs @@ -185,6 +185,23 @@ impl RawPacket { } } +pub enum ImprovPacket { + CurrentStateResponse(CurrentStateResponse), + RequestCurrentStateCommand(RequestCurrentStateCommand), +} + +impl ImprovPacket { + pub fn try_from_raw_packet(raw_packet: &RawPacket) -> Result { + match raw_packet.r#type { + PacketType::CurrentState => Ok(ImprovPacket::CurrentStateResponse(CurrentStateResponse::try_from_raw_packet(raw_packet)?)), + //PacketType::ErrorState => _, + //PacketType::RPCCommand => _, + //PacketType::RPCResult => _, + _ => Err("Conversion into packet type {} not implemented"), + } + } +} + pub struct CurrentStateResponse { pub current_state: CurrentState, } diff --git a/src/main.rs b/src/main.rs index abbeef3..613eaee 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,14 +11,13 @@ use hex; use improv_setup::improv::{ IMPROV_HEADER, IMPROV_VERSION, - PacketType, RPCCommand, CurrentState, calculate_checksum, ImprovDataToPacket, ImprovDataFromPacket, RawPacket, - CurrentStateResponse, + ImprovPacket, RequestCurrentStateCommand, }; use log::{ @@ -149,9 +148,7 @@ async fn main() -> Result<()>{ // type println!("Type: {}", &raw_packet.r#type); - if raw_packet.r#type == PacketType::CurrentState { - let current_state_response = CurrentStateResponse::try_from_raw_packet(&raw_packet).unwrap(); - + if let ImprovPacket::CurrentStateResponse(current_state_response) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() { println!("Current state: {}", ¤t_state_response.current_state); }