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