feat: domo_lib
This commit is contained in:
parent
a3dfb5d7ea
commit
8194499ce2
6 changed files with 203 additions and 0 deletions
11
domo_lib/src/lib.rs
Normal file
11
domo_lib/src/lib.rs
Normal file
|
@ -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;
|
26
domo_lib/src/node/mod.rs
Normal file
26
domo_lib/src/node/mod.rs
Normal file
|
@ -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<String, Property>,
|
||||
/// Domo protocol allows for nodes under nodes therefore there is a hashmap for it.
|
||||
pub nodes: HashMap<Identifier, Node>
|
||||
}
|
||||
|
||||
impl Node {
|
||||
pub fn new(identifier: Identifier) -> Self {
|
||||
Self {
|
||||
identifier,
|
||||
properties: HashMap::new(),
|
||||
nodes: HashMap::new()
|
||||
}
|
||||
}
|
||||
}
|
6
domo_lib/src/node/property.rs
Normal file
6
domo_lib/src/node/property.rs
Normal file
|
@ -0,0 +1,6 @@
|
|||
use domo_proto::data_types::DataType;
|
||||
|
||||
pub struct Property {
|
||||
pub name: String,
|
||||
pub value: DataType
|
||||
}
|
59
domo_lib/src/packet.rs
Normal file
59
domo_lib/src/packet.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
use domo_proto::packet::Packet;
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
|
||||
pub fn read_packet<R>(stream: &mut R) -> io::Result<Packet>
|
||||
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::<Vec<u8>>::into(p.clone()));
|
||||
buf.extend(Into::<Vec<u8>>::into(p.clone()));
|
||||
|
||||
let mut cursor = Cursor::new(buf);
|
||||
|
||||
assert_eq!(
|
||||
Into::<Vec<u8>>::into(read_packet(&mut cursor).unwrap()),
|
||||
Into::<Vec<u8>>::into(p),
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue