From 1bce2af93c19da939b40392b287c4218f5d78625 Mon Sep 17 00:00:00 2001 From: Michael Goldenberg Date: Thu, 12 Jun 2025 11:44:49 -0400 Subject: [PATCH] refactor(indexeddb): add trait for converting between high-level types and indexed types in event cache store Signed-off-by: Michael Goldenberg --- .../src/event_cache_store/mod.rs | 2 + .../src/event_cache_store/serializer/mod.rs | 1 + .../event_cache_store/serializer/traits.rs | 44 +++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/traits.rs diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs index 11a97c5a7..9211bdb0c 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs @@ -12,6 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License +#![allow(unused)] + mod migrations; mod serializer; mod types; diff --git a/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/mod.rs b/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/mod.rs index 8766e82b0..f294a4287 100644 --- a/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/mod.rs +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/mod.rs @@ -12,4 +12,5 @@ // See the License for the specific language governing permissions and // limitations under the License +mod traits; mod types; 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 new file mode 100644 index 000000000..ef51d4429 --- /dev/null +++ b/crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/traits.rs @@ -0,0 +1,44 @@ +// Copyright 2025 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License + +use ruma::RoomId; + +use crate::serializer::IndexeddbSerializer; + +/// A conversion trait for preparing high-level types into indexed types +/// which are better suited for storage in IndexedDB. +/// +/// Note that the functions below take an [`IndexeddbSerializer`] as an +/// argument, which provides the necessary context for encryption and +/// decryption, in the case the high-level type must be encrypted before +/// storage. +pub trait Indexed: Sized { + /// The indexed type that is used for storage in IndexedDB. + type IndexedType; + /// The error type that is returned when conversion fails. + type Error; + + /// Converts the high-level type into an indexed type. + fn to_indexed( + &self, + room_id: &RoomId, + serializer: &IndexeddbSerializer, + ) -> Result; + + /// Converts an indexed type into the high-level type. + fn from_indexed( + indexed: Self::IndexedType, + serializer: &IndexeddbSerializer, + ) -> Result; +}