matrix_sdk_indexeddb/
lib.rs

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