aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl_server/src/net
diff options
context:
space:
mode:
authorFuwn <[email protected]>2021-06-06 00:09:18 +0000
committerFuwn <[email protected]>2021-06-06 00:09:18 +0000
commite46da84bd264993d32a45b88bd5d51af0c6ef5ed (patch)
tree7588d6b85683a339deb65fab248b3927012157e3 /crates/whirl_server/src/net
parentrefactor(whirl_server): remove single imports (diff)
downloadwhirl-e46da84bd264993d32a45b88bd5d51af0c6ef5ed.tar.xz
whirl-e46da84bd264993d32a45b88bd5d51af0c6ef5ed.zip
refactor(whirl_server::net): move orphan functions to methods within property list struct
Diffstat (limited to 'crates/whirl_server/src/net')
-rw-r--r--crates/whirl_server/src/net/converter.rs58
-rw-r--r--crates/whirl_server/src/net/mod.rs7
-rw-r--r--crates/whirl_server/src/net/network_property.rs (renamed from crates/whirl_server/src/net/structure.rs)6
-rw-r--r--crates/whirl_server/src/net/property_list.rs94
-rw-r--r--crates/whirl_server/src/net/property_parser.rs34
5 files changed, 99 insertions, 100 deletions
diff --git a/crates/whirl_server/src/net/converter.rs b/crates/whirl_server/src/net/converter.rs
deleted file mode 100644
index f8a5188..0000000
--- a/crates/whirl_server/src/net/converter.rs
+++ /dev/null
@@ -1,58 +0,0 @@
-// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
-// SPDX-License-Identifier: GPL-3.0-only
-
-use bytes::{BufMut, BytesMut};
-use num_traits::AsPrimitive;
-
-use crate::{
- cmd::constants::Command,
- net::{
- constants::{PROPACCESS_POSSESS, PROPFLAG_DBSTORE},
- PropertyList,
- },
-};
-
-pub fn property_list_to_bytes(
- command_id: i32,
- obj_id: i32,
- mut property_list: PropertyList,
-) -> Vec<u8> {
- let mut command = BytesMut::new();
-
- // Iterate over all network properties
- loop {
- // Check if there are any properties left
- trace!("props left: {}", property_list.len());
- if property_list.is_empty() {
- break;
- }
-
- let property = &property_list[0]; // Property we are currently iterating over
- trace!("current prop: {}:{}", property.prop_id, property.value);
-
- command.put_u8(property.prop_id.as_(): u8); // Property ID
-
- // NOTE: THIS IS SUPER BAD DO NOT DO THIS! But it works!
- if command_id == Command::PropUpd as i32 {
- command.put_u8(PROPFLAG_DBSTORE.as_(): u8); // Flag (s)
- command.put_u8(PROPACCESS_POSSESS.as_(): u8); // Access
- }
-
- command.put_u8(property.value.len().as_(): u8); // Property UTF-8 Length
- command.put_slice(property.value.as_bytes()); // Property UTF-8
-
- property_list.reverse();
- property_list.pop();
- property_list.reverse();
- }
-
- // Convert to vector and insert the header
- let mut command_as_vec = command.to_vec();
-
- command_as_vec.insert(0, command_id.as_(): u8); // Command ID
- command_as_vec.insert(0, obj_id.as_(): u8); // ObjId
- command_as_vec.insert(0, command.len().as_(): u8 + 3); // Data length
-
- // Return bytes
- command_as_vec
-}
diff --git a/crates/whirl_server/src/net/mod.rs b/crates/whirl_server/src/net/mod.rs
index 27e4cf5..32f9995 100644
--- a/crates/whirl_server/src/net/mod.rs
+++ b/crates/whirl_server/src/net/mod.rs
@@ -2,8 +2,5 @@
// SPDX-License-Identifier: GPL-3.0-only
pub mod constants;
-pub mod converter;
-pub mod property_parser;
-pub mod structure;
-
-type PropertyList = Vec<crate::net::structure::NetworkProperty>;
+pub mod network_property;
+pub mod property_list;
diff --git a/crates/whirl_server/src/net/structure.rs b/crates/whirl_server/src/net/network_property.rs
index 1549340..259c83b 100644
--- a/crates/whirl_server/src/net/structure.rs
+++ b/crates/whirl_server/src/net/network_property.rs
@@ -5,9 +5,9 @@ pub struct NetworkProperty {
pub prop_id: i32,
pub value: String,
}
-// impl NetworkProperty {
-// pub fn new() -> Self { NetworkProperty::default() }
-// }
+impl NetworkProperty {
+ pub fn _new() -> Self { Self::default() }
+}
impl Default for NetworkProperty {
fn default() -> Self {
Self {
diff --git a/crates/whirl_server/src/net/property_list.rs b/crates/whirl_server/src/net/property_list.rs
new file mode 100644
index 0000000..4bba6c7
--- /dev/null
+++ b/crates/whirl_server/src/net/property_list.rs
@@ -0,0 +1,94 @@
+// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
+// SPDX-License-Identifier: GPL-3.0-only
+
+use bytes::{BufMut, BytesMut};
+use num_traits::AsPrimitive;
+
+use crate::{
+ cmd::constants::Command,
+ net::{
+ constants::{PROPACCESS_POSSESS, PROPFLAG_DBSTORE},
+ network_property::NetworkProperty,
+ },
+};
+
+pub struct PropertyList(pub Vec<crate::net::network_property::NetworkProperty>);
+impl PropertyList {
+ pub fn as_bytes(&mut self, command_id: i32, obj_id: i32) -> Vec<u8> {
+ let mut command = BytesMut::new();
+ let property_list = &mut self.0;
+
+ // Iterate over all network properties
+ loop {
+ // Check if there are any properties left
+ trace!("props left: {}", property_list.len());
+ if property_list.is_empty() {
+ break;
+ }
+
+ let property = &property_list[0]; // Property we are currently iterating over
+ trace!("current prop: {}:{}", property.prop_id, property.value);
+
+ command.put_u8(property.prop_id.as_(): u8); // Property ID
+
+ // NOTE: THIS IS SUPER BAD DO NOT DO THIS! But it works!
+ if command_id == Command::PropUpd as i32 {
+ command.put_u8(PROPFLAG_DBSTORE.as_(): u8); // Flag (s)
+ command.put_u8(PROPACCESS_POSSESS.as_(): u8); // Access
+ }
+
+ command.put_u8(property.value.len().as_(): u8); // Property UTF-8 Length
+ command.put_slice(property.value.as_bytes()); // Property UTF-8
+
+ property_list.reverse();
+ property_list.pop();
+ property_list.reverse();
+ }
+
+ // Convert to vector and insert the header
+ let mut command_as_vec = command.to_vec();
+
+ command_as_vec.insert(0, command_id.as_(): u8); // Command ID
+ command_as_vec.insert(0, obj_id.as_(): u8); // ObjId
+ command_as_vec.insert(0, command.len().as_(): u8 + 3); // Data length
+
+ // Return bytes
+ command_as_vec
+ }
+
+ pub fn find(&self, property: i32) -> &NetworkProperty {
+ self.0.iter().find(|i| i.prop_id == property).unwrap()
+ }
+
+ /// Iterate over a network property in the form of bytes (Vec<u8>) and return
+ /// a list of human-readable properties.
+ pub fn from_bytes(mut data: Vec<u8>) -> Self {
+ let mut property_list = vec![];
+
+ // Iterate over all network properties
+ loop {
+ // Check if any commands are present
+ if data.len() <= 2 {
+ break;
+ }
+ trace!("iteration: {:?}", data);
+ // if data[0] == 0 {
+ // break;
+ // }
+
+ let property_length = data[1] + 2;
+ property_list.push(crate::net::network_property::NetworkProperty {
+ prop_id: i32::from(data[0]),
+ value: std::str::from_utf8(&data[2..data[1] as usize + 2])
+ .unwrap()
+ .to_string(),
+ });
+
+ // Remove current property from the network property
+ data = data[property_length as usize..].to_vec();
+ }
+
+ // Return the human-readable network property
+ Self(property_list)
+ }
+}
diff --git a/crates/whirl_server/src/net/property_parser.rs b/crates/whirl_server/src/net/property_parser.rs
deleted file mode 100644
index 9e04d1c..0000000
--- a/crates/whirl_server/src/net/property_parser.rs
+++ /dev/null
@@ -1,34 +0,0 @@
-// Copyleft (ɔ) 2021-2021 The Whirlsplash Collective
-// SPDX-License-Identifier: GPL-3.0-only
-
-/// Iterate over a network property in the form of bytes (Vec<u8>) and return a
-/// list of human-readable properties.
-pub fn parse_network_property(mut data: Vec<u8>) -> crate::net::PropertyList {
- let mut property_list = vec![];
-
- // Iterate over all network properties
- loop {
- // Check if any commands are present
- if data.len() <= 2 {
- break;
- }
- trace!("iteration: {:?}", data);
- // if data[0] == 0 {
- // break;
- // }
-
- let property_length = data[1] + 2;
- property_list.push(crate::net::structure::NetworkProperty {
- prop_id: i32::from(data[0]),
- value: std::str::from_utf8(&data[2..data[1] as usize + 2])
- .unwrap()
- .to_string(),
- });
-
- // Remove current property from the network property
- data = data[property_length as usize..].to_vec();
- }
-
- // Return the human-readable network property
- property_list
-}