From 5e64da660ce63ae47921a478fb0ef57789980d03 Mon Sep 17 00:00:00 2001 From: Michael Goldenberg Date: Mon, 23 Jun 2025 10:22:48 -0400 Subject: [PATCH] refactor(indexeddb): add default impls for IndexedKeyBounds::{encode_lower, encode_upper} Signed-off-by: Michael Goldenberg --- .../event_cache_store/serializer/traits.rs | 14 +++- .../src/event_cache_store/serializer/types.rs | 77 ------------------- 2 files changed, 12 insertions(+), 79 deletions(-) diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/traits.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/traits.rs index d602f8124..7572fc972 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/traits.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/traits.rs @@ -72,11 +72,21 @@ pub trait IndexedKeyBounds: IndexedKey { fn lower_key_components() -> Self::KeyComponents; /// Encodes the lower bound of the key. - fn encode_lower(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self; + fn encode_lower(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self + where + Self: Sized, + { + >::encode(room_id, &Self::lower_key_components(), serializer) + } /// Constructs the upper bound of the key components. fn upper_key_components() -> Self::KeyComponents; /// Encodes the upper bound of the key. - fn encode_upper(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self; + fn encode_upper(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self + where + Self: Sized, + { + >::encode(room_id, &Self::upper_key_components(), serializer) + } } diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/types.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/types.rs index f25431962..0f4d40e0e 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/types.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/types.rs @@ -139,18 +139,6 @@ impl IndexedKeyBounds for IndexedChunkIdKey { fn upper_key_components() -> Self::KeyComponents { ChunkIdentifier::new(js_sys::Number::MAX_SAFE_INTEGER as u64) } - - fn encode_lower(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self { - >::encode(room_id, &ChunkIdentifier::new(0), serializer) - } - - fn encode_upper(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self { - >::encode( - room_id, - &ChunkIdentifier::new(js_sys::Number::MAX_SAFE_INTEGER as u64), - serializer, - ) - } } pub type IndexedRoomId = String; @@ -216,18 +204,6 @@ impl IndexedKeyBounds for IndexedNextChunkIdKey { fn upper_key_components() -> Self::KeyComponents { Some(ChunkIdentifier::new(js_sys::Number::MAX_SAFE_INTEGER as u64)) } - - fn encode_lower(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self { - >::encode(room_id, &None, serializer) - } - - fn encode_upper(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self { - >::encode( - room_id, - &Some(ChunkIdentifier::new(js_sys::Number::MAX_SAFE_INTEGER as u64)), - serializer, - ) - } } /// Represents the [`EVENTS`][1] object store. @@ -315,18 +291,6 @@ impl IndexedKeyBounds for IndexedEventIdKey { fn upper_key_components() -> Self::KeyComponents { OwnedEventId::try_from(format!("${INDEXED_KEY_UPPER_CHARACTER}")).expect("valid event id") } - - fn encode_lower(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self { - let room_id = serializer.encode_key_as_string(keys::ROOMS, room_id); - let event_id = String::from(INDEXED_KEY_LOWER_CHARACTER); - Self(room_id, event_id) - } - - fn encode_upper(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self { - let room_id = serializer.encode_key_as_string(keys::ROOMS, room_id); - let event_id = String::from(INDEXED_KEY_UPPER_CHARACTER); - Self(room_id, event_id) - } } pub type IndexedEventId = String; @@ -362,25 +326,6 @@ impl IndexedKeyBounds for IndexedEventPositionKey { index: js_sys::Number::MAX_SAFE_INTEGER as usize, } } - - fn encode_lower(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self { - >::encode( - room_id, - &Position { chunk_identifier: 0, index: 0 }, - serializer, - ) - } - - fn encode_upper(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self { - >::encode( - room_id, - &Position { - chunk_identifier: js_sys::Number::MAX_SAFE_INTEGER as u64, - index: js_sys::Number::MAX_SAFE_INTEGER as usize, - }, - serializer, - ) - } } pub type IndexedEventPositionIndex = usize; @@ -429,20 +374,6 @@ impl IndexedKeyBounds for IndexedEventRelationKey { RelationType::from(INDEXED_KEY_UPPER_CHARACTER.to_string()), ) } - - fn encode_lower(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self { - let room_id = serializer.encode_key_as_string(keys::ROOMS, room_id); - let related_event_id = String::from(INDEXED_KEY_LOWER_CHARACTER); - let relation_type = String::from(INDEXED_KEY_LOWER_CHARACTER); - Self(room_id, related_event_id, relation_type) - } - - fn encode_upper(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self { - let room_id = serializer.encode_key_as_string(keys::ROOMS, room_id); - let related_event_id = String::from(INDEXED_KEY_UPPER_CHARACTER); - let relation_type = String::from(INDEXED_KEY_UPPER_CHARACTER); - Self(room_id, related_event_id, relation_type) - } } /// A representation of the relationship between two events (see @@ -517,14 +448,6 @@ impl IndexedKeyBounds for IndexedGapIdKey { fn upper_key_components() -> Self::KeyComponents { >::upper_key_components() } - - fn encode_lower(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self { - >::encode_lower(room_id, serializer) - } - - fn encode_upper(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self { - >::encode_upper(room_id, serializer) - } } pub type IndexedGapContent = MaybeEncrypted;