From 3caff2dd58d84a9e4e259c6da596807f025de88c Mon Sep 17 00:00:00 2001 From: Raine Date: Wed, 28 Feb 2024 15:01:34 +0100 Subject: [PATCH] feat: traits traits traits --- domo_proto/Cargo.lock | 2 +- domo_proto/Cargo.toml | 2 +- domo_proto/src/commands/mod.rs | 4 ++++ domo_proto/src/commands/node_management/mod.rs | 5 +++-- domo_proto/src/commands/property_control/mod.rs | 5 +++-- domo_proto/src/commands/raw_data_transmission/mod.rs | 10 ++++++++++ domo_proto/src/packet/mod.rs | 9 +++++++++ 7 files changed, 31 insertions(+), 6 deletions(-) diff --git a/domo_proto/Cargo.lock b/domo_proto/Cargo.lock index 2090f24..26507b9 100644 --- a/domo_proto/Cargo.lock +++ b/domo_proto/Cargo.lock @@ -19,7 +19,7 @@ dependencies = [ [[package]] name = "domo_proto" -version = "1.0.1" +version = "1.0.2" dependencies = [ "crc32fast", "rand", diff --git a/domo_proto/Cargo.toml b/domo_proto/Cargo.toml index bb6eb33..9264064 100644 --- a/domo_proto/Cargo.toml +++ b/domo_proto/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "domo_proto" -version = "1.0.1" +version = "1.0.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/domo_proto/src/commands/mod.rs b/domo_proto/src/commands/mod.rs index a82cdfe..dd866c6 100644 --- a/domo_proto/src/commands/mod.rs +++ b/domo_proto/src/commands/mod.rs @@ -10,6 +10,10 @@ pub mod property_control; /// This is the abstraction for sending raw data over domo. pub mod raw_data_transmission; +pub trait PacketCommand { + fn command(&self) -> u8; +} + impl_into_enum_variant!(PacketData::NodeManagement, node_management::NodeManagementCommand); impl_into_enum_variant!(PacketData::PropertyControl, property_control::PropertyControlCommand); impl_into_enum_variant!(PacketData::Unknown, Vec); diff --git a/domo_proto/src/commands/node_management/mod.rs b/domo_proto/src/commands/node_management/mod.rs index fb32d98..981234b 100644 --- a/domo_proto/src/commands/node_management/mod.rs +++ b/domo_proto/src/commands/node_management/mod.rs @@ -1,4 +1,5 @@ use std::io; +use crate::commands::PacketCommand; use crate::identifier::Identifier; use crate::packet::raw_packet::RawPacket; use crate::packet::{Packet, ToPacket}; @@ -21,8 +22,8 @@ pub enum NodeManagementCommand { }, } -impl NodeManagementCommand { - pub fn command(&self) -> u8 { +impl PacketCommand for NodeManagementCommand { + fn command(&self) -> u8 { match self { NodeManagementCommand::Ping => 0x00, NodeManagementCommand::RegisterNode { .. } => 0x01, diff --git a/domo_proto/src/commands/property_control/mod.rs b/domo_proto/src/commands/property_control/mod.rs index 870b8f9..44fcc36 100644 --- a/domo_proto/src/commands/property_control/mod.rs +++ b/domo_proto/src/commands/property_control/mod.rs @@ -4,6 +4,7 @@ use crate::packet::packet_data::PacketData; use crate::packet::raw_packet::RawPacket; use crate::packet::{Packet, ToPacket}; use std::io; +use crate::commands::PacketCommand; pub mod vec; @@ -39,8 +40,8 @@ pub enum PropertyControlCommand { }, } -impl PropertyControlCommand { - pub fn command(&self) -> u8 { +impl PacketCommand for PropertyControlCommand { + fn command(&self) -> u8 { match self { PropertyControlCommand::Register { .. } => 0x10, PropertyControlCommand::Remove { .. } => 0x11, diff --git a/domo_proto/src/commands/raw_data_transmission/mod.rs b/domo_proto/src/commands/raw_data_transmission/mod.rs index 8ff1d2e..218eb18 100644 --- a/domo_proto/src/commands/raw_data_transmission/mod.rs +++ b/domo_proto/src/commands/raw_data_transmission/mod.rs @@ -4,6 +4,7 @@ use crate::{ identifier::Identifier, packet::{packet_data::PacketData, raw_packet::RawPacket, Packet, ToPacket}, }; +use crate::commands::PacketCommand; pub mod vec; @@ -25,6 +26,15 @@ pub enum RawDataTransmission { }, } +impl PacketCommand for RawDataTransmission { + fn command(&self) -> u8 { + match self { + RawDataTransmission::SetupTransmission { .. } => 0xF0, + RawDataTransmission::Data { .. } => 0xF1 + } + } +} + impl ToPacket for RawDataTransmission { fn to_packet( self, diff --git a/domo_proto/src/packet/mod.rs b/domo_proto/src/packet/mod.rs index 6fa29b9..835775d 100644 --- a/domo_proto/src/packet/mod.rs +++ b/domo_proto/src/packet/mod.rs @@ -46,6 +46,15 @@ impl Default for Packet { } /// Secretly calls RawPacket::try_from().into() + +impl TryFrom for Packet { + type Error = io::Error; + + fn try_from(value: RawPacket) -> Result { + value.into() + } +} + impl TryFrom> for Packet { type Error = io::Error; fn try_from(data: Vec) -> io::Result {