From 83d64e0879205b4a78129eac97562b17de6b72df Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 26 Sep 2025 05:32:30 +0000 Subject: [PATCH] Implement Deserialize for RawPduId for database convenience. Add room equality convenience on RawPduId. Signed-off-by: Jason Volk --- src/core/matrix/pdu/raw_id.rs | 71 ++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 18 deletions(-) diff --git a/src/core/matrix/pdu/raw_id.rs b/src/core/matrix/pdu/raw_id.rs index 0fe3d9c7..35e37575 100644 --- a/src/core/matrix/pdu/raw_id.rs +++ b/src/core/matrix/pdu/raw_id.rs @@ -1,6 +1,7 @@ use std::fmt; use arrayvec::ArrayVec; +use serde::{Deserialize, Deserializer}; use super::{Count, Id, ShortEventId, ShortId, ShortRoomId}; @@ -13,6 +14,8 @@ pub enum RawId { type RawIdNormal = [u8; RawId::NORMAL_LEN]; type RawIdBackfilled = [u8; RawId::BACKFILLED_LEN]; +struct RawIdVisitor; + const INT_LEN: usize = size_of::(); impl RawId { @@ -20,6 +23,10 @@ impl RawId { const MAX_LEN: usize = Self::BACKFILLED_LEN; const NORMAL_LEN: usize = size_of::() + size_of::(); + #[inline] + #[must_use] + pub fn is_room_eq(self, other: Self) -> bool { self.shortroomid() == other.shortroomid() } + #[inline] #[must_use] pub fn pdu_count(&self) -> Count { @@ -70,28 +77,22 @@ impl fmt::Debug for RawId { } } -impl AsRef<[u8]> for RawId { +impl<'de> Deserialize<'de> for RawId { #[inline] - fn as_ref(&self) -> &[u8] { self.as_bytes() } + fn deserialize>(d: D) -> Result { + d.deserialize_bytes(RawIdVisitor) + } } -impl From<&[u8]> for RawId { - #[inline] - fn from(id: &[u8]) -> Self { - match id.len() { - | Self::NORMAL_LEN => Self::Normal( - id[0..Self::NORMAL_LEN] - .try_into() - .expect("normal RawId from [u8]"), - ), - | Self::BACKFILLED_LEN => Self::Backfilled( - id[0..Self::BACKFILLED_LEN] - .try_into() - .expect("backfilled RawId from [u8]"), - ), - | _ => unimplemented!("unrecognized RawId length"), - } +impl serde::de::Visitor<'_> for RawIdVisitor { + type Value = RawId; + + fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("RawId byte array") } + + #[inline] + fn visit_bytes(self, buf: &[u8]) -> Result { Ok(RawId::from(buf)) } } impl From for RawId { @@ -124,3 +125,37 @@ impl From for RawId { } } } + +impl From<&[u8]> for RawId { + #[inline] + fn from(id: &[u8]) -> Self { + match id.len() { + | Self::NORMAL_LEN => Self::Normal( + id[0..Self::NORMAL_LEN] + .try_into() + .expect("normal RawId from [u8]"), + ), + | Self::BACKFILLED_LEN => Self::Backfilled( + id[0..Self::BACKFILLED_LEN] + .try_into() + .expect("backfilled RawId from [u8]"), + ), + | _ => unimplemented!("unrecognized RawId length"), + } + } +} + +impl From<&[u8; Self::NORMAL_LEN]> for RawId { + #[inline] + fn from(id: &[u8; Self::NORMAL_LEN]) -> Self { Self::Normal(*id) } +} + +impl From<&[u8; Self::BACKFILLED_LEN]> for RawId { + #[inline] + fn from(id: &[u8; Self::BACKFILLED_LEN]) -> Self { Self::Backfilled(*id) } +} + +impl AsRef<[u8]> for RawId { + #[inline] + fn as_ref(&self) -> &[u8] { self.as_bytes() } +}