sdk-base: Add constructors with stores for StoreConfig

This commit is contained in:
Kévin Commaille
2022-03-10 14:26:27 +01:00
parent 81605b731d
commit 597999acba
4 changed files with 23 additions and 14 deletions

View File

@@ -641,6 +641,24 @@ impl StoreConfig {
Default::default()
}
/// Create a new store config wrapping the given state store
pub fn new_with_state_store(state_store: Box<dyn StateStore>) -> Self {
StoreConfig {
state_store: Some(state_store),
#[cfg(feature = "encryption")]
crypto_store: Default::default(),
}
}
/// Create a new store config wrapping the given state and crypto store
#[cfg(feature = "encryption")]
pub fn new_with_state_and_crypto_store(
state_store: Box<dyn StateStore>,
crypto_store: Box<dyn CryptoStore>,
) -> Self {
StoreConfig { state_store: Some(state_store), crypto_store: Some(crypto_store) }
}
/// Set a custom implementation of a `CryptoStore`.
///
/// The crypto store must be opened before being set.

View File

@@ -45,14 +45,12 @@ async fn make_store_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);
Ok(StoreConfig::new_with_state_and_crypto_store(state_store, crypto_store))
}
#[cfg(not(feature = "encryption"))]
@@ -63,8 +61,6 @@ async fn make_store_config(
StateStore::open_with_name(name).await?
};
config = config.state_store(Box::new(state_store));
Ok(StoreConfig::new_with_state_store(Box::new(state_store)))
}
Ok(config)
}

View File

@@ -35,13 +35,10 @@ pub fn make_store_config(
path: impl AsRef<Path>,
passphrase: Option<&str>,
) -> Result<StoreConfig, anyhow::Error> {
let mut config = StoreConfig::new();
#[cfg(feature = "encryption")]
{
let (state_store, crypto_store) = open_stores_with_path(path, passphrase)?;
config = config.state_store(state_store);
config = config.crypto_store(crypto_store);
Ok(StoreConfig::new_with_state_and_crypto_store(state_store, crypto_store))
}
#[cfg(not(feature = "encryption"))]
@@ -52,8 +49,6 @@ pub fn make_store_config(
StateStore::open_with_path(path)?
};
config = config.state_store(Box::new(state_store));
Ok(StoreConfig::new_with_state_store(Box::new(state_store)))
}
Ok(config)
}

View File

@@ -121,7 +121,7 @@ impl ClientConfig {
/// # let custom_state_store = Box::new(MemoryStore::new());
/// use matrix_sdk::{Client, config::{ClientConfig, StoreConfig}};
///
/// let store_config = StoreConfig::new().state_store(custom_state_store);
/// let store_config = StoreConfig::new_with_state_store(custom_state_store);
/// let client_config = ClientConfig::with_store_config(store_config)
/// .use_discovery_response();
///