split string utils into unit

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-07-03 00:47:58 +00:00
parent 52d470058a
commit af81baae44
3 changed files with 33 additions and 25 deletions

View file

@ -5,6 +5,7 @@ pub mod hash;
pub mod html; pub mod html;
pub mod json; pub mod json;
pub mod mutex_map; pub mod mutex_map;
pub mod string;
pub mod sys; pub mod sys;
mod tests; mod tests;
@ -18,8 +19,7 @@ pub use hash::calculate_hash;
pub use html::Escape as HtmlEscape; pub use html::Escape as HtmlEscape;
pub use json::{deserialize_from_str, to_canonical_object}; pub use json::{deserialize_from_str, to_canonical_object};
pub use mutex_map::MutexMap; pub use mutex_map::MutexMap;
use rand::prelude::*; pub use string::{random_string, str_from_bytes, string_from_bytes};
use ruma::UserId;
pub use sys::available_parallelism; pub use sys::available_parallelism;
use crate::Result; use crate::Result;
@ -54,34 +54,12 @@ pub fn generate_keypair() -> Vec<u8> {
value 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<String> {
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. /// Parses the bytes into an u64.
pub fn u64_from_bytes(bytes: &[u8]) -> Result<u64> { pub fn u64_from_bytes(bytes: &[u8]) -> Result<u64> {
let array: [u8; 8] = bytes.try_into()?; let array: [u8; 8] = bytes.try_into()?;
Ok(u64::from_be_bytes(array)) 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()
} }

22
src/core/utils/string.rs Normal file
View file

@ -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<String> {
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)?) }

View file

@ -130,8 +130,16 @@ fn presenceid_key(count: u64, user_id: &UserId) -> Vec<u8> {
#[inline] #[inline]
fn presenceid_parse(key: &[u8]) -> Result<(u64, &UserId)> { fn presenceid_parse(key: &[u8]) -> Result<(u64, &UserId)> {
let (count, user_id) = key.split_at(8); 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(); let count = utils::u64_from_bytes(count).unwrap();
Ok((count, user_id)) 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)
}