From 8194499ce2afa089345d73a15eb9f4c98c0abd15 Mon Sep 17 00:00:00 2001 From: Raine Date: Sun, 5 Nov 2023 20:52:51 +0100 Subject: [PATCH] feat: domo_lib --- domo_lib/Cargo.lock | 92 +++++++++++++++++++++++++++++++++++ domo_lib/Cargo.toml | 9 ++++ domo_lib/src/lib.rs | 11 +++++ domo_lib/src/node/mod.rs | 26 ++++++++++ domo_lib/src/node/property.rs | 6 +++ domo_lib/src/packet.rs | 59 ++++++++++++++++++++++ 6 files changed, 203 insertions(+) create mode 100644 domo_lib/Cargo.lock create mode 100644 domo_lib/Cargo.toml create mode 100644 domo_lib/src/lib.rs create mode 100644 domo_lib/src/node/mod.rs create mode 100644 domo_lib/src/node/property.rs create mode 100644 domo_lib/src/packet.rs diff --git a/domo_lib/Cargo.lock b/domo_lib/Cargo.lock new file mode 100644 index 0000000..e19f7eb --- /dev/null +++ b/domo_lib/Cargo.lock @@ -0,0 +1,92 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "crc32fast" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" +dependencies = [ + "cfg-if", +] + +[[package]] +name = "domo_lib" +version = "0.1.0" +dependencies = [ + "domo_proto", +] + +[[package]] +name = "domo_proto" +version = "0.2.0" +dependencies = [ + "crc32fast", + "rand", +] + +[[package]] +name = "getrandom" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "be4136b2a15dd319360be1c07d9933517ccf0be8f16bf62a3bee4f0d618df427" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.150" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c" + +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + +[[package]] +name = "wasi" +version = "0.11.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" diff --git a/domo_lib/Cargo.toml b/domo_lib/Cargo.toml new file mode 100644 index 0000000..fc126d2 --- /dev/null +++ b/domo_lib/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "domo_lib" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +domo_proto = { path = "../domo_proto" } \ No newline at end of file diff --git a/domo_lib/src/lib.rs b/domo_lib/src/lib.rs new file mode 100644 index 0000000..5f847be --- /dev/null +++ b/domo_lib/src/lib.rs @@ -0,0 +1,11 @@ +//! Welcome to the Domo Library. +//! +//! This is a library full of abstractions and helpers for the Domo protocol library. + +#![forbid(clippy::unwrap_used, clippy::expect_used)] + +/// All abstractions and helpers for the packet struct. +pub mod packet; + +/// Node structs and helpers +pub mod node; \ No newline at end of file diff --git a/domo_lib/src/node/mod.rs b/domo_lib/src/node/mod.rs new file mode 100644 index 0000000..81a471e --- /dev/null +++ b/domo_lib/src/node/mod.rs @@ -0,0 +1,26 @@ +use std::collections::HashMap; +use domo_proto::identifier::Identifier; +use self::property::Property; + +pub mod property; + +pub struct Node { + // Identifier of the node + pub identifier: Identifier, + /// According to the domo protocol spec: + /// Strings have a limit of 32 bytes. + /// Theoretically there is a limit of 0xFFFFFFFF (4294967295) properties. + pub properties: HashMap, + /// Domo protocol allows for nodes under nodes therefore there is a hashmap for it. + pub nodes: HashMap +} + +impl Node { + pub fn new(identifier: Identifier) -> Self { + Self { + identifier, + properties: HashMap::new(), + nodes: HashMap::new() + } + } +} \ No newline at end of file diff --git a/domo_lib/src/node/property.rs b/domo_lib/src/node/property.rs new file mode 100644 index 0000000..4a58b1e --- /dev/null +++ b/domo_lib/src/node/property.rs @@ -0,0 +1,6 @@ +use domo_proto::data_types::DataType; + +pub struct Property { + pub name: String, + pub value: DataType +} \ No newline at end of file diff --git a/domo_lib/src/packet.rs b/domo_lib/src/packet.rs new file mode 100644 index 0000000..e5470b3 --- /dev/null +++ b/domo_lib/src/packet.rs @@ -0,0 +1,59 @@ +use domo_proto::packet::Packet; +use std::io; +use std::io::Read; + +pub fn read_packet(stream: &mut R) -> io::Result +where + R: Read, +{ + let version = { + let mut buf = [0_u8; 1]; + stream.read_exact(&mut buf)?; + buf[0] + }; + + match version { + 1 => { + let header = { + let mut buf = [0_u8; 0x13]; // this is 0x14 (header length) - 1 (version) + stream.read_exact(&mut buf)?; + buf + }; + + let size = u16::from_be_bytes([header[0x10], header[0x11]]); // relative to header piece + + let mut packet_buf = vec![]; + packet_buf.push(version); + packet_buf.extend(header); + + stream + .take(size as u64 + 4) // create new read instance and make sure we do not read more than allocated. (body and checksum) + .read_to_end(&mut packet_buf)?; // read to end into packet_buf + + Ok(Packet::try_from(packet_buf)?) + } + _ => Err(io::Error::new( + io::ErrorKind::InvalidData, + "Unsupported version", + )), + } +} + +#[test] +/// check if packet is the same when created and read +fn test_read_packet() { + use std::io::Cursor; + use domo_proto::packet::Packet; + let p = Packet::default(); + + let mut buf = vec![]; + buf.extend(Into::>::into(p.clone())); + buf.extend(Into::>::into(p.clone())); + + let mut cursor = Cursor::new(buf); + + assert_eq!( + Into::>::into(read_packet(&mut cursor).unwrap()), + Into::>::into(p), + ); +} \ No newline at end of file