matrix_sdk_indexeddb/
lib.rs

1#![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 error;
10#[cfg(feature = "event-cache-store")]
11mod event_cache_store;
12#[cfg(feature = "media-store")]
13mod media_store;
14mod serializer;
15#[cfg(feature = "state-store")]
16mod state_store;
17#[cfg(any(feature = "event-cache-store", feature = "media-store"))]
18mod transaction;
19
20#[cfg(feature = "e2e-encryption")]
21pub use crypto_store::{IndexeddbCryptoStore, IndexeddbCryptoStoreError};
22#[cfg(feature = "state-store")]
23pub use state_store::{
24    IndexeddbStateStore, IndexeddbStateStoreBuilder, IndexeddbStateStoreError,
25    MigrationConflictStrategy,
26};
27
28/// Create a [`IndexeddbStateStore`] and a [`IndexeddbCryptoStore`] that use the
29/// same name and passphrase.
30#[cfg(all(feature = "e2e-encryption", feature = "state-store"))]
31pub async fn open_stores_with_name(
32    name: &str,
33    passphrase: Option<&str>,
34) -> Result<(IndexeddbStateStore, IndexeddbCryptoStore), OpenStoreError> {
35    let mut builder = IndexeddbStateStore::builder().name(name.to_owned());
36    if let Some(passphrase) = passphrase {
37        builder = builder.passphrase(passphrase.to_owned());
38    }
39
40    let state_store = builder.build().await.map_err(StoreError::from)?;
41    let crypto_store =
42        IndexeddbCryptoStore::open_with_store_cipher(name, state_store.store_cipher.clone())
43            .await?;
44
45    Ok((state_store, crypto_store))
46}
47
48/// Create an [`IndexeddbStateStore`].
49///
50/// If a `passphrase` is given, the store will be encrypted using a key derived
51/// from that passphrase.
52#[cfg(feature = "state-store")]
53pub async fn open_state_store(
54    name: &str,
55    passphrase: Option<&str>,
56) -> Result<IndexeddbStateStore, OpenStoreError> {
57    let mut builder = IndexeddbStateStore::builder().name(name.to_owned());
58    if let Some(passphrase) = passphrase {
59        builder = builder.passphrase(passphrase.to_owned());
60    }
61    let state_store = builder.build().await.map_err(StoreError::from)?;
62
63    Ok(state_store)
64}
65
66/// All the errors that can occur when opening an IndexedDB store.
67#[derive(Error, Debug)]
68pub enum OpenStoreError {
69    /// An error occurred with the state store implementation.
70    #[cfg(feature = "state-store")]
71    #[error(transparent)]
72    State(#[from] StoreError),
73
74    /// An error occurred with the crypto store implementation.
75    #[cfg(feature = "e2e-encryption")]
76    #[error(transparent)]
77    Crypto(#[from] IndexeddbCryptoStoreError),
78}