|
| 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