sled: Add a method to create a StoreConfig

This commit is contained in:
Kévin Commaille
2022-03-09 13:53:09 +01:00
parent 0ea12b3b4a
commit 4b7f05e913
2 changed files with 33 additions and 2 deletions

View File

@@ -11,7 +11,7 @@ required-features = ["binary-build"]
[features]
default = ["encryption"]
encryption = ["matrix-sdk-crypto"]
encryption = ["matrix-sdk-base/encryption", "matrix-sdk-crypto"]
binary-build = [
"atty",
"clap",

View File

@@ -1,6 +1,7 @@
#[cfg(feature = "encryption")]
use std::path::Path;
use matrix_sdk_base::store::StoreConfig;
#[cfg(feature = "encryption")]
mod cryptostore;
mod state_store;
@@ -26,3 +27,33 @@ pub fn open_stores_with_path(
Ok((Box::new(state_store), Box::new(crypto_store)))
}
}
/// Create a [`StoreConfig`] with an opened sled [`StateStore`] that uses the
/// given path and passphrase. If `encryption` is enabled, a [`CryptoStore`]
/// with the same parameters is also opened.
pub fn make_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);
}
#[cfg(not(feature = "encryption"))]
{
let state_store = if let Some(passphrase) = passphrase {
StateStore::open_with_passphrase(path, passphrase)?
} else {
StateStore::open_with_path(path)?
};
config = config.state_store(Box::new(state_store));
}
Ok(config)
}