feat: traits traits traits
Some checks failed
ci/woodpecker/push/proto Pipeline failed

This commit is contained in:
Strix 2024-02-28 15:01:34 +01:00
parent b38cb3022b
commit 3caff2dd58
No known key found for this signature in database
GPG key ID: 5F35B3B8537287A7
7 changed files with 31 additions and 6 deletions

2
domo_proto/Cargo.lock generated
View file

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

View file

@ -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

View file

@ -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<u8>);

View file

@ -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,

View file

@ -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,

View file

@ -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,

View file

@ -46,6 +46,15 @@ impl Default for Packet {
}
/// Secretly calls RawPacket::try_from().into()
impl TryFrom<RawPacket> for Packet {
type Error = io::Error;
fn try_from(value: RawPacket) -> Result<Self, Self::Error> {
value.into()
}
}
impl TryFrom<Vec<u8>> for Packet {
type Error = io::Error;
fn try_from(data: Vec<u8>) -> io::Result<Self> {