diff --git a/src/core/error.rs b/src/core/error.rs index 6942d1be..ac2e176d 100644 --- a/src/core/error.rs +++ b/src/core/error.rs @@ -28,6 +28,12 @@ pub enum Error { Fmt(#[from] fmt::Error), #[error("I/O error: {0}")] Io(#[from] std::io::Error), + #[error("{0}")] + Utf8Error(#[from] std::str::Utf8Error), + #[error("{0}")] + FromUtf8Error(#[from] std::string::FromUtf8Error), + #[error("{0}")] + TryFromSliceError(#[from] std::array::TryFromSliceError), // third-party #[error("Regex error: {0}")] diff --git a/src/core/utils/mod.rs b/src/core/utils/mod.rs index 1e0e01c0..bbaaf2ec 100644 --- a/src/core/utils/mod.rs +++ b/src/core/utils/mod.rs @@ -19,10 +19,10 @@ pub use json::{deserialize_from_str, to_canonical_object}; pub use mutex_map::MutexMap; use rand::prelude::*; use ring::digest; -use ruma::OwnedUserId; +use ruma::UserId; pub use sys::available_parallelism; -use crate::{Error, Result}; +use crate::Result; pub fn clamp(val: T, min: T, max: T) -> T { cmp::min(cmp::max(val, min), max) } @@ -38,9 +38,8 @@ pub fn millis_since_unix_epoch() -> u64 { #[inline] #[must_use] pub fn increment(old: Option<&[u8]>) -> [u8; 8] { - const ZERO: u64 = 0; old.map(TryInto::try_into) - .map_or(ZERO, |val| val.map_or(ZERO, u64::from_be_bytes)) + .map_or(0_u64, |val| val.map_or(0_u64, u64::from_be_bytes)) .wrapping_add(1) .to_be_bytes() } @@ -55,23 +54,26 @@ pub fn generate_keypair() -> Vec { value } -/// 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)) +/// 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::from_utf8(bytes.to_vec()) +pub fn string_from_bytes(bytes: &[u8]) -> Result { + let str: &str = str_from_bytes(bytes)?; + Ok(str.to_owned()) } -/// Parses a `OwnedUserId` from bytes. -pub fn user_id_from_bytes(bytes: &[u8]) -> Result { - OwnedUserId::try_from( - string_from_bytes(bytes).map_err(|_| Error::bad_database("Failed to parse string from bytes"))?, - ) - .map_err(|_| Error::bad_database("Failed to parse user id from bytes")) +/// 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 { diff --git a/src/service/presence/data.rs b/src/service/presence/data.rs index 5cb4032f..42b827d3 100644 --- a/src/service/presence/data.rs +++ b/src/service/presence/data.rs @@ -115,7 +115,7 @@ impl Data { .iter() .flat_map(|(key, presence_bytes)| -> Result<(OwnedUserId, u64, Vec)> { let (count, user_id) = presenceid_parse(&key)?; - Ok((user_id, count, presence_bytes)) + Ok((user_id.to_owned(), count, presence_bytes)) }) .filter(move |(_, count, _)| *count > since), ) @@ -128,7 +128,7 @@ fn presenceid_key(count: u64, user_id: &UserId) -> Vec { } #[inline] -fn presenceid_parse(key: &[u8]) -> Result<(u64, OwnedUserId)> { +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 count = utils::u64_from_bytes(count).unwrap();