indexeddb: Add methods to create a StoreConfig

This commit is contained in:
Kévin Commaille
2022-03-09 14:30:49 +01:00
parent 4b7f05e913
commit 0327b4f8fc
2 changed files with 57 additions and 1 deletions

View File

@@ -5,7 +5,7 @@ edition = "2021"
[features]
default = ["encryption"]
encryption = ["matrix-sdk-crypto"]
encryption = ["matrix-sdk-base/encryption", "matrix-sdk-crypto"]
[package.metadata.docs.rs]
default-target = "wasm32-unknown-unknown"

View File

@@ -1,3 +1,6 @@
#[cfg(target_arch = "wasm32")]
use matrix_sdk_base::store::StoreConfig;
mod safe_encode;
#[cfg(target_arch = "wasm32")]
@@ -12,3 +15,56 @@ mod cryptostore;
pub use cryptostore::IndexeddbStore as CryptoStore;
#[cfg(target_arch = "wasm32")]
pub use state_store::IndexeddbStore as StateStore;
#[cfg(target_arch = "wasm32")]
#[cfg(feature = "encryption")]
/// Create a [`StateStore`] and a [`CryptoStore`] that use the same name and
/// passphrase.
pub async fn open_stores_with_name(
name: impl Into<String>,
passphrase: Option<&str>,
) -> Result<(Box<StateStore>, Box<CryptoStore>), anyhow::Error> {
let name = name.into();
if let Some(passphrase) = passphrase {
let state_store = StateStore::open_with_passphrase(name.clone(), passphrase).await?;
let crypto_store = CryptoStore::open_with_passphrase(name, passphrase).await?;
Ok((Box::new(state_store), Box::new(crypto_store)))
} else {
let state_store = StateStore::open_with_name(name.clone()).await?;
let crypto_store = CryptoStore::open_with_name(name).await?;
Ok((Box::new(state_store), Box::new(crypto_store)))
}
}
#[cfg(target_arch = "wasm32")]
/// Create a [`StoreConfig`] with an opened indexeddb [`StateStore`] that uses
/// the given name and passphrase. If `encryption` is enabled, a [`CryptoStore`]
/// with the same parameters is also opened.
pub async fn make_config(
name: impl Into<String>,
passphrase: Option<&str>,
) -> Result<StoreConfig, anyhow::Error> {
let mut config = StoreConfig::new();
let name = name.into();
#[cfg(feature = "encryption")]
{
let (state_store, crypto_store) = open_stores_with_name(name, passphrase).await?;
config = config.state_store(state_store);
config = config.crypto_store(crypto_store);
}
#[cfg(not(feature = "encryption"))]
{
let state_store = if let Some(passphrase) = passphrase {
StateStore::open_with_passphrase(name, passphrase).await?
} else {
StateStore::open_with_name(name).await?
};
config = config.state_store(Box::new(state_store));
}
Ok(config)
}