Skip to content

Commit 1bce2af

Browse files
mgoldenbergHywan
authored andcommitted
refactor(indexeddb): add trait for converting between high-level types and indexed types in event cache store
Signed-off-by: Michael Goldenberg <m@mgoldenberg.net>
1 parent 47c8df0 commit 1bce2af

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

crates/matrix-sdk-indexeddb/src/event_cache_store/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License
1414

15+
#![allow(unused)]
16+
1517
mod migrations;
1618
mod serializer;
1719
mod types;

crates/matrix-sdk-indexeddb/src/event_cache_store/serializer/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License
1414

15+
mod traits;
1516
mod types;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2025 The Matrix.org Foundation C.I.C.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License
14+
15+
use ruma::RoomId;
16+
17+
use crate::serializer::IndexeddbSerializer;
18+
19+
/// A conversion trait for preparing high-level types into indexed types
20+
/// which are better suited for storage in IndexedDB.
21+
///
22+
/// Note that the functions below take an [`IndexeddbSerializer`] as an
23+
/// argument, which provides the necessary context for encryption and
24+
/// decryption, in the case the high-level type must be encrypted before
25+
/// storage.
26+
pub trait Indexed: Sized {
27+
/// The indexed type that is used for storage in IndexedDB.
28+
type IndexedType;
29+
/// The error type that is returned when conversion fails.
30+
type Error;
31+
32+
/// Converts the high-level type into an indexed type.
33+
fn to_indexed(
34+
&self,
35+
room_id: &RoomId,
36+
serializer: &IndexeddbSerializer,
37+
) -> Result<Self::IndexedType, Self::Error>;
38+
39+
/// Converts an indexed type into the high-level type.
40+
fn from_indexed(
41+
indexed: Self::IndexedType,
42+
serializer: &IndexeddbSerializer,
43+
) -> Result<Self, Self::Error>;
44+
}

0 commit comments

Comments
 (0)