diff options
| author | Fuwn <[email protected]> | 2024-06-12 01:28:50 +0000 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2024-06-12 01:28:50 +0000 |
| commit | 2f824aaa357a74390bff6410c4b0b3295162105e (patch) | |
| tree | dd94a9553112165d9e3835862cb45a6d1ac75499 /crates/whirl_server/src/cmd/commands/register_object_id.rs | |
| parent | refactor(crates): update idioms (diff) | |
| download | whirl-2f824aaa357a74390bff6410c4b0b3295162105e.tar.xz whirl-2f824aaa357a74390bff6410c4b0b3295162105e.zip | |
feat(whirl_server): short object id sending
Diffstat (limited to 'crates/whirl_server/src/cmd/commands/register_object_id.rs')
| -rw-r--r-- | crates/whirl_server/src/cmd/commands/register_object_id.rs | 30 |
1 files changed, 27 insertions, 3 deletions
diff --git a/crates/whirl_server/src/cmd/commands/register_object_id.rs b/crates/whirl_server/src/cmd/commands/register_object_id.rs index bdf4bca..8c73d8c 100644 --- a/crates/whirl_server/src/cmd/commands/register_object_id.rs +++ b/crates/whirl_server/src/cmd/commands/register_object_id.rs @@ -2,15 +2,21 @@ // SPDX-License-Identifier: GPL-3.0-only use { - crate::cmd::{constants::Command, extendable::Creatable}, - bytes::{BufMut, BytesMut}, + crate::cmd::{ + constants::Command, + extendable::{Creatable, Parsable}, + }, + byteorder::ReadBytesExt, + bytes::{Buf, BufMut, BytesMut}, + std::io::Read, }; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct RegisterObjectId { pub long_object_id: String, pub short_object_id: i8, } + impl Creatable for RegisterObjectId { fn create(&self) -> Vec<u8> { let mut command = BytesMut::new(); @@ -33,3 +39,21 @@ impl Creatable for RegisterObjectId { command_as_vec } } + +impl Parsable for RegisterObjectId { + fn parse(data: Vec<u8>) -> Self { + // https://stackoverflow.com/questions/41034635/how-do-i-convert-between-string-str-vecu8-and-u8 + let mut data = BytesMut::from(data.as_slice()).reader(); + + // Content + let long_object_id_length = data.read_u8().unwrap() as usize; + let mut long_object_id = vec![0; long_object_id_length]; + data.read_exact(&mut long_object_id).unwrap(); + let short_object_id = data.read_i8().unwrap(); + + Self { + long_object_id: String::from_utf8(long_object_id).unwrap(), + short_object_id, + } + } +} |