From e63d2792293579213bed554a78664ac70d9acc9f Mon Sep 17 00:00:00 2001 From: clerie Date: Wed, 18 Dec 2024 20:14:25 +0100 Subject: [PATCH] Rename ImprovPacket to RawPacket --- src/improv.rs | 16 ++++++++-------- src/main.rs | 14 +++++++------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/improv.rs b/src/improv.rs index 71067c0..cd5e3e9 100644 --- a/src/improv.rs +++ b/src/improv.rs @@ -111,8 +111,8 @@ pub trait ImprovDataPacketType { pub trait ImprovDataToPacket: ImprovDataPacketType { fn to_bytes(&self) -> Vec; - fn to_packet(&self) -> ImprovPacket { - return ImprovPacket { + fn to_raw_packet(&self) -> RawPacket { + return RawPacket { version: IMPROV_VERSION, r#type: Self::packet_type, data: self.to_bytes().to_vec(), @@ -124,16 +124,16 @@ pub trait ImprovDataToPacket: ImprovDataPacketType { pub trait ImprovDataFromPacket: ImprovDataPacketType + Sized { type Error; - fn try_from_packet(improv_packet: &ImprovPacket) -> Result; + fn try_from_raw_packet(raw_packet: &RawPacket) -> Result; } -pub struct ImprovPacket { +pub struct RawPacket { pub version: u8, pub r#type: PacketType, pub data: Vec, } -impl ImprovPacket { +impl RawPacket { pub fn try_from_bytes(bytes: &Vec) -> Result { if bytes.len() < 11 { return Err("Packet too small"); @@ -204,13 +204,13 @@ impl ImprovDataToPacket for CurrentStateResponse { impl ImprovDataFromPacket for CurrentStateResponse { type Error = &'static str; - fn try_from_packet(improv_packet: &ImprovPacket) -> Result{ - if improv_packet.r#type != Self::packet_type { + fn try_from_raw_packet(raw_packet: &RawPacket) -> Result{ + if raw_packet.r#type != Self::packet_type { return Err("Packet is not CurrentStateResponse"); } return Ok(Self { - current_state: CurrentState::try_from(&improv_packet.data[0])?, + current_state: CurrentState::try_from(&raw_packet.data[0])?, }) } } diff --git a/src/main.rs b/src/main.rs index 61f1906..abbeef3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,7 +17,7 @@ use improv_setup::improv::{ calculate_checksum, ImprovDataToPacket, ImprovDataFromPacket, - ImprovPacket, + RawPacket, CurrentStateResponse, RequestCurrentStateCommand, }; @@ -121,7 +121,7 @@ async fn main() -> Result<()>{ .fold(String::new(), |a, b| a + &b + &String::from("\n"))); }, Commands::Device {path, baud_rate, device_command} => { - let request_current_state_packet = (RequestCurrentStateCommand {}).to_packet(); + let request_current_state_packet = (RequestCurrentStateCommand {}).to_raw_packet(); println!("{}", hex::encode(&request_current_state_packet.to_bytes())); println!("{}", to_ascii_debug(&request_current_state_packet.to_bytes())); @@ -141,16 +141,16 @@ async fn main() -> Result<()>{ println!("{}", improv_packet_offset); let improv_packet_end = improv_packet_offset + 10 + >::into(buffer[improv_packet_offset+8]); - let improv_packet = ImprovPacket::try_from_bytes(&buffer[improv_packet_offset..improv_packet_end].to_vec()).unwrap(); + let raw_packet = RawPacket::try_from_bytes(&buffer[improv_packet_offset..improv_packet_end].to_vec()).unwrap(); // version - println!("Version: {}", &improv_packet.version); + println!("Version: {}", &raw_packet.version); // type - println!("Type: {}", &improv_packet.r#type); + println!("Type: {}", &raw_packet.r#type); - if improv_packet.r#type == PacketType::CurrentState { - let current_state_response = CurrentStateResponse::try_from_packet(&improv_packet).unwrap(); + if raw_packet.r#type == PacketType::CurrentState { + let current_state_response = CurrentStateResponse::try_from_raw_packet(&raw_packet).unwrap(); println!("Current state: {}", ¤t_state_response.current_state); }