refactor(indexeddb): add default impls for IndexedKeyBounds::{encode_lower, encode_upper}

Signed-off-by: Michael Goldenberg <m@mgoldenberg.net>
This commit is contained in:
Michael Goldenberg
2025-06-23 10:22:48 -04:00
committed by Ivan Enderlin
parent d152ce13a0
commit 5e64da660c
2 changed files with 12 additions and 79 deletions

View File

@@ -72,11 +72,21 @@ pub trait IndexedKeyBounds<T: Indexed>: IndexedKey<T> {
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,
{
<Self as IndexedKey<T>>::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,
{
<Self as IndexedKey<T>>::encode(room_id, &Self::upper_key_components(), serializer)
}
}

View File

@@ -139,18 +139,6 @@ impl IndexedKeyBounds<Chunk> 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 {
<Self as IndexedKey<Chunk>>::encode(room_id, &ChunkIdentifier::new(0), serializer)
}
fn encode_upper(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self {
<Self as IndexedKey<Chunk>>::encode(
room_id,
&ChunkIdentifier::new(js_sys::Number::MAX_SAFE_INTEGER as u64),
serializer,
)
}
}
pub type IndexedRoomId = String;
@@ -216,18 +204,6 @@ impl IndexedKeyBounds<Chunk> 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 {
<Self as IndexedKey<Chunk>>::encode(room_id, &None, serializer)
}
fn encode_upper(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self {
<Self as IndexedKey<Chunk>>::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<Event> 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<Event> for IndexedEventPositionKey {
index: js_sys::Number::MAX_SAFE_INTEGER as usize,
}
}
fn encode_lower(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self {
<Self as IndexedKey<Event>>::encode(
room_id,
&Position { chunk_identifier: 0, index: 0 },
serializer,
)
}
fn encode_upper(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self {
<Self as IndexedKey<Event>>::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<Event> 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<Gap> for IndexedGapIdKey {
fn upper_key_components() -> Self::KeyComponents {
<Self as IndexedKeyBounds<Chunk>>::upper_key_components()
}
fn encode_lower(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self {
<IndexedChunkIdKey as IndexedKeyBounds<Chunk>>::encode_lower(room_id, serializer)
}
fn encode_upper(room_id: &RoomId, serializer: &IndexeddbSerializer) -> Self {
<IndexedChunkIdKey as IndexedKeyBounds<Chunk>>::encode_upper(room_id, serializer)
}
}
pub type IndexedGapContent = MaybeEncrypted;