fix: remove clunky code

This commit is contained in:
Strix 2024-02-27 14:13:19 +01:00
parent 4be6659662
commit b38cb3022b
No known key found for this signature in database
GPG key ID: 5F35B3B8537287A7
5 changed files with 51 additions and 36 deletions

2
domo_proto/Cargo.lock generated
View file

@ -19,7 +19,7 @@ dependencies = [
[[package]]
name = "domo_proto"
version = "1.0.0"
version = "1.0.1"
dependencies = [
"crc32fast",
"rand",

View file

@ -1,6 +1,6 @@
[package]
name = "domo_proto"
version = "1.0.0"
version = "1.0.1"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View file

@ -3,7 +3,6 @@ use crate::identifier::Identifier;
use crate::packet::packet_data::PacketData;
use crate::packet::raw_packet::RawPacket;
use crate::packet::{Packet, ToPacket};
use crate::prelude::as_u32_be;
use std::io;
pub mod vec;
@ -149,9 +148,14 @@ impl TryFrom<RawPacket> for PropertyControlCommand {
0x1A => {
if raw_packet.data_length == 36 {
Ok(PropertyControlCommand::Subscribe {
device_id: Identifier::from(as_u32_be(
raw_packet.data[0x14..0x18].as_ref(),
)),
device_id: Identifier::from(
u32::from_be_bytes([
raw_packet.data[0x14],
raw_packet.data[0x15],
raw_packet.data[0x16],
raw_packet.data[0x17],
])
),
property_name: String::from_utf8(raw_packet.data[0x18..0x4C].to_vec())
.map_err(|_| {
io::Error::new(io::ErrorKind::InvalidInput, "String is not UTF-8")
@ -165,9 +169,14 @@ impl TryFrom<RawPacket> for PropertyControlCommand {
0x1B => {
if raw_packet.data_length == 36 {
Ok(PropertyControlCommand::Unsubscribe {
device_id: Identifier::from(as_u32_be(
raw_packet.data[0x14..0x18].as_ref(),
)),
device_id: Identifier::from(
u32::from_be_bytes([
raw_packet.data[0x14],
raw_packet.data[0x15],
raw_packet.data[0x16],
raw_packet.data[0x17],
])
),
property_name: String::from_utf8(raw_packet.data[0x18..0x4C].to_vec())
.map_err(|_| {
io::Error::new(io::ErrorKind::InvalidInput, "String is not UTF-8")

View file

@ -1,29 +1,53 @@
use std::io;
use crate::packet::PACKET_HEADER_SIZE;
use crate::packet::raw_packet::RawPacket;
use crate::prelude::as_u32_be;
impl TryFrom<Vec<u8>> for RawPacket {
type Error = io::Error;
fn try_from(data: Vec<u8>) -> io::Result<Self> {
if data.len() < PACKET_HEADER_SIZE {
return Err(io::Error::new(io::ErrorKind::InvalidData, "Can't parse data into RawPacket"))
return Err(io::Error::new(io::ErrorKind::InvalidData, "Can't parse data into RawPacket"));
}
let data_length = (((data[0x12] as u16) << 8) | data[0x13] as u16) as usize;
if data.len() < 0x14 + data_length {
return Err(io::Error::new(io::ErrorKind::InvalidData, "Can't parse data into RawPacket"))
return Err(io::Error::new(io::ErrorKind::InvalidData, "Can't parse data into RawPacket"));
}
let checksum = as_u32_be(data[(0x14 + data_length)..].as_ref());
let checksum = u32::from_be_bytes([
data[0x14 + data_length],
data[0x14 + data_length + 1],
data[0x14 + data_length + 2],
data[0x14 + data_length + 3],
]);
let built_checksum = crc32fast::hash(data[..(data.len() - 4)].as_ref());
if checksum != built_checksum {
return Err(io::Error::new(io::ErrorKind::InvalidData, format!("Checksum does not match {checksum:X?} != {built_checksum:X?}")))
return Err(io::Error::new(io::ErrorKind::InvalidData, format!("Checksum does not match {checksum:X?} != {built_checksum:X?}")));
}
Ok(RawPacket {
version: data[0],
dest: as_u32_be(data[0x01..0x05].as_ref()),
src: as_u32_be(data[0x05..0x09].as_ref()),
packet_id: as_u32_be(data[0x09..0x0D].as_ref()),
reply_to: as_u32_be(data[0x0D..0x11].as_ref()),
dest: u32::from_be_bytes([
data[0x01],
data[0x02],
data[0x03],
data[0x04]
]),
src: u32::from_be_bytes([
data[0x05],
data[0x06],
data[0x07],
data[0x08],
]),
packet_id: u32::from_be_bytes([
data[0x09],
data[0x0A],
data[0x0B],
data[0x0C],
]),
reply_to: u32::from_be_bytes([
data[0x0D],
data[0x0E],
data[0x0F],
data[0x10],
]),
command: data[0x11],
data_length,
data: data[0x14..(0x14 + data_length)].to_vec(),

View file

@ -1,21 +1,3 @@
pub fn as_u32_be(array: &[u8]) -> u32 {
((array[0] as u32) << 24)
+ ((array[1] as u32) << 16)
+ ((array[2] as u32) << 8)
+ ((array[3] as u32) << 0)
}
pub fn as_u64_be(array: &[u8]) -> u64 {
((array[0] as u64) << 56)
+ ((array[1] as u64) << 48)
+ ((array[2] as u64) << 40)
+ ((array[3] as u64) << 32)
+ ((array[4] as u64) << 24)
+ ((array[5] as u64) << 16)
+ ((array[6] as u64) << 8)
+ ((array[7] as u64) << 0)
}
macro_rules! impl_into_enum_variant {
($houser: tt::$variant: tt, $impl_type: ty) => {
impl Into<$houser> for $impl_type {