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