matrix_sdk_indexeddb/
lib.rs1#![cfg_attr(not(target_family = "wasm"), allow(unused))]
2
3#[cfg(feature = "state-store")]
4use matrix_sdk_base::store::StoreError;
5use thiserror::Error;
6
7#[cfg(feature = "e2e-encryption")]
8mod crypto_store;
9mod durability;
10mod error;
11#[cfg(feature = "event-cache-store")]
12mod event_cache_store;
13#[cfg(feature = "media-store")]
14mod media_store;
15mod serializer;
16#[cfg(feature = "state-store")]
17mod state_store;
18#[cfg(any(feature = "event-cache-store", feature = "media-store"))]
19mod transaction;
20
21#[cfg(feature = "e2e-encryption")]
22pub use crypto_store::{IndexeddbCryptoStore, IndexeddbCryptoStoreError};
23#[cfg(feature = "state-store")]
24pub use state_store::{
25 IndexeddbStateStore, IndexeddbStateStoreBuilder, IndexeddbStateStoreError,
26 MigrationConflictStrategy,
27};
28
29#[cfg(feature = "event-cache-store")]
30pub use crate::event_cache_store::{
31 IndexeddbEventCacheStore, IndexeddbEventCacheStoreBuilder, IndexeddbEventCacheStoreError,
32};
33#[cfg(feature = "media-store")]
34pub use crate::media_store::{
35 IndexeddbMediaStore, IndexeddbMediaStoreBuilder, IndexeddbMediaStoreError,
36};
37
38pub struct IndexeddbStores {
44 #[cfg(feature = "e2e-encryption")]
48 pub crypto: IndexeddbCryptoStore,
49 #[cfg(feature = "state-store")]
53 pub state: IndexeddbStateStore,
54 #[cfg(feature = "event-cache-store")]
58 pub event_cache: IndexeddbEventCacheStore,
59 #[cfg(feature = "media-store")]
63 pub media: IndexeddbMediaStore,
64}
65
66impl IndexeddbStores {
67 pub async fn open(
79 name: &str,
80 #[allow(unused_variables)] passphrase: Option<&str>,
81 ) -> Result<Self, OpenStoreError> {
82 #[cfg(all(feature = "e2e-encryption", feature = "state-store"))]
83 if let Some(passphrase) = passphrase {
84 return Self::open_with_passphrase(name, passphrase).await;
85 }
86
87 Self::open_without_passphrase(name).await
88 }
89
90 #[allow(clippy::unused_async)]
96 pub async fn open_without_passphrase(name: &str) -> Result<Self, OpenStoreError> {
97 #[cfg(feature = "state-store")]
98 let state = IndexeddbStateStore::builder()
99 .name(name.to_owned())
100 .build()
101 .await
102 .map_err(StoreError::from)?;
103
104 #[cfg(feature = "e2e-encryption")]
105 let crypto = IndexeddbCryptoStore::open_with_name(name).await?;
106
107 #[cfg(feature = "event-cache-store")]
108 let event_cache = IndexeddbEventCacheStoreBuilder::with_prefix(name).build().await?;
109
110 #[cfg(feature = "media-store")]
111 let media = IndexeddbMediaStoreBuilder::with_prefix(name).build().await?;
112
113 Ok(Self {
114 #[cfg(feature = "state-store")]
115 state,
116 #[cfg(feature = "e2e-encryption")]
117 crypto,
118 #[cfg(feature = "event-cache-store")]
119 event_cache,
120 #[cfg(feature = "media-store")]
121 media,
122 })
123 }
124
125 #[cfg(all(feature = "e2e-encryption", feature = "state-store"))]
135 pub async fn open_with_passphrase(
136 name: &str,
137 passphrase: &str,
138 ) -> Result<Self, OpenStoreError> {
139 let state = IndexeddbStateStore::builder()
140 .name(name.to_owned())
141 .passphrase(passphrase.to_owned())
142 .build()
143 .await
144 .map_err(StoreError::from)?;
145 let store_cipher =
146 state.store_cipher.clone().ok_or(OpenStoreError::FailedToLoadStoreCipher)?;
147
148 let crypto =
149 IndexeddbCryptoStore::open_with_store_cipher(name, Some(store_cipher.clone())).await?;
150
151 #[cfg(feature = "event-cache-store")]
152 let event_cache = IndexeddbEventCacheStoreBuilder::with_prefix(name)
153 .store_cipher(store_cipher.clone())
154 .build()
155 .await?;
156
157 #[cfg(feature = "media-store")]
158 let media = IndexeddbMediaStoreBuilder::with_prefix(name)
159 .store_cipher(store_cipher.clone())
160 .build()
161 .await?;
162
163 Ok(Self {
164 state,
165 crypto,
166 #[cfg(feature = "event-cache-store")]
167 event_cache,
168 #[cfg(feature = "media-store")]
169 media,
170 })
171 }
172}
173
174#[deprecated(
177 note = "this function only opens state and crypto stores, use `IndexeddbStores::open()` instead."
178)]
179#[cfg(all(feature = "e2e-encryption", feature = "state-store"))]
180pub async fn open_stores_with_name(
181 name: &str,
182 passphrase: Option<&str>,
183) -> Result<(IndexeddbStateStore, IndexeddbCryptoStore), OpenStoreError> {
184 let mut builder = IndexeddbStateStore::builder().name(name.to_owned());
185 if let Some(passphrase) = passphrase {
186 builder = builder.passphrase(passphrase.to_owned());
187 }
188
189 let state_store = builder.build().await.map_err(StoreError::from)?;
190 let crypto_store =
191 IndexeddbCryptoStore::open_with_store_cipher(name, state_store.store_cipher.clone())
192 .await?;
193
194 Ok((state_store, crypto_store))
195}
196
197#[cfg(feature = "state-store")]
202pub async fn open_state_store(
203 name: &str,
204 passphrase: Option<&str>,
205) -> Result<IndexeddbStateStore, OpenStoreError> {
206 let mut builder = IndexeddbStateStore::builder().name(name.to_owned());
207 if let Some(passphrase) = passphrase {
208 builder = builder.passphrase(passphrase.to_owned());
209 }
210 let state_store = builder.build().await.map_err(StoreError::from)?;
211
212 Ok(state_store)
213}
214
215#[derive(Error, Debug)]
217pub enum OpenStoreError {
218 #[cfg(feature = "state-store")]
220 #[error(transparent)]
221 State(#[from] StoreError),
222
223 #[cfg(feature = "e2e-encryption")]
225 #[error(transparent)]
226 Crypto(#[from] IndexeddbCryptoStoreError),
227
228 #[cfg(feature = "e2e-encryption")]
233 #[error("failed to load store cipher")]
234 FailedToLoadStoreCipher,
235
236 #[cfg(feature = "event-cache-store")]
238 #[error(transparent)]
239 Event(#[from] IndexeddbEventCacheStoreError),
240
241 #[cfg(feature = "media-store")]
243 #[error(transparent)]
244 Media(#[from] IndexeddbMediaStoreError),
245}