Convert RawPacket into ImprovPacket

This commit is contained in:
clerie 2024-12-18 21:00:09 +01:00
parent e63d279229
commit 8f6fbb4778
2 changed files with 19 additions and 5 deletions

View File

@ -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<Self, &str> {
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 struct CurrentStateResponse {
pub current_state: CurrentState, pub current_state: CurrentState,
} }

View File

@ -11,14 +11,13 @@ use hex;
use improv_setup::improv::{ use improv_setup::improv::{
IMPROV_HEADER, IMPROV_HEADER,
IMPROV_VERSION, IMPROV_VERSION,
PacketType,
RPCCommand, RPCCommand,
CurrentState, CurrentState,
calculate_checksum, calculate_checksum,
ImprovDataToPacket, ImprovDataToPacket,
ImprovDataFromPacket, ImprovDataFromPacket,
RawPacket, RawPacket,
CurrentStateResponse, ImprovPacket,
RequestCurrentStateCommand, RequestCurrentStateCommand,
}; };
use log::{ use log::{
@ -149,9 +148,7 @@ async fn main() -> Result<()>{
// type // type
println!("Type: {}", &raw_packet.r#type); println!("Type: {}", &raw_packet.r#type);
if raw_packet.r#type == PacketType::CurrentState { if let ImprovPacket::CurrentStateResponse(current_state_response) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() {
let current_state_response = CurrentStateResponse::try_from_raw_packet(&raw_packet).unwrap();
println!("Current state: {}", &current_state_response.current_state); println!("Current state: {}", &current_state_response.current_state);
} }