From af81baae441047d65c544b7118cf5a197b54ad70 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Wed, 3 Jul 2024 00:47:58 +0000 Subject: [PATCH] split string utils into unit Signed-off-by: Jason Volk --- src/core/utils/mod.rs | 26 ++------------------------ src/core/utils/string.rs | 22 ++++++++++++++++++++++ src/service/presence/data.rs | 10 +++++++++- 3 files changed, 33 insertions(+), 25 deletions(-) create mode 100644 src/core/utils/string.rs diff --git a/src/core/utils/mod.rs b/src/core/utils/mod.rs index 721ed977..1ff36b02 100644 --- a/src/core/utils/mod.rs +++ b/src/core/utils/mod.rs @@ -5,6 +5,7 @@ pub mod hash; pub mod html; pub mod json; pub mod mutex_map; +pub mod string; pub mod sys; mod tests; @@ -18,8 +19,7 @@ pub use hash::calculate_hash; pub use html::Escape as HtmlEscape; pub use json::{deserialize_from_str, to_canonical_object}; pub use mutex_map::MutexMap; -use rand::prelude::*; -use ruma::UserId; +pub use string::{random_string, str_from_bytes, string_from_bytes}; pub use sys::available_parallelism; use crate::Result; @@ -54,34 +54,12 @@ pub fn generate_keypair() -> Vec { value } -/// Parses a `UserId` from bytes. -pub fn user_id_from_bytes(bytes: &[u8]) -> Result<&UserId> { - let str: &str = str_from_bytes(bytes)?; - let user_id: &UserId = str.try_into()?; - Ok(user_id) -} - -/// Parses the bytes into a string. -pub fn string_from_bytes(bytes: &[u8]) -> Result { - let str: &str = str_from_bytes(bytes)?; - Ok(str.to_owned()) -} - -/// Parses the bytes into a string. -pub fn str_from_bytes(bytes: &[u8]) -> Result<&str> { Ok(std::str::from_utf8(bytes)?) } - /// Parses the bytes into an u64. pub fn u64_from_bytes(bytes: &[u8]) -> Result { let array: [u8; 8] = bytes.try_into()?; Ok(u64::from_be_bytes(array)) } -pub fn random_string(length: usize) -> String { - thread_rng() - .sample_iter(&rand::distributions::Alphanumeric) - .take(length) - .map(char::from) - .collect() } diff --git a/src/core/utils/string.rs b/src/core/utils/string.rs new file mode 100644 index 00000000..7f6f6531 --- /dev/null +++ b/src/core/utils/string.rs @@ -0,0 +1,22 @@ +use rand::prelude::*; + +use crate::Result; + +pub fn random_string(length: usize) -> String { + thread_rng() + .sample_iter(&rand::distributions::Alphanumeric) + .take(length) + .map(char::from) + .collect() +} + +/// Parses the bytes into a string. +#[inline] +pub fn string_from_bytes(bytes: &[u8]) -> Result { + let str: &str = str_from_bytes(bytes)?; + Ok(str.to_owned()) +} + +/// Parses the bytes into a string. +#[inline] +pub fn str_from_bytes(bytes: &[u8]) -> Result<&str> { Ok(std::str::from_utf8(bytes)?) } diff --git a/src/service/presence/data.rs b/src/service/presence/data.rs index 42b827d3..dac8becf 100644 --- a/src/service/presence/data.rs +++ b/src/service/presence/data.rs @@ -130,8 +130,16 @@ fn presenceid_key(count: u64, user_id: &UserId) -> Vec { #[inline] fn presenceid_parse(key: &[u8]) -> Result<(u64, &UserId)> { let (count, user_id) = key.split_at(8); - let user_id = utils::user_id_from_bytes(user_id)?; + let user_id = user_id_from_bytes(user_id)?; let count = utils::u64_from_bytes(count).unwrap(); Ok((count, user_id)) } + +/// Parses a `UserId` from bytes. +fn user_id_from_bytes(bytes: &[u8]) -> Result<&UserId> { + let str: &str = utils::str_from_bytes(bytes)?; + let user_id: &UserId = str.try_into()?; + + Ok(user_id) +}