From 4b7f05e91349ce41696ce907b8dbd86def409da3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Wed, 9 Mar 2022 13:53:09 +0100 Subject: [PATCH] sled: Add a method to create a StoreConfig --- crates/matrix-sdk-sled/Cargo.toml | 2 +- crates/matrix-sdk-sled/src/lib.rs | 33 ++++++++++++++++++++++++++++++- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/crates/matrix-sdk-sled/Cargo.toml b/crates/matrix-sdk-sled/Cargo.toml index 8fd3bee6b..1b1c4e938 100644 --- a/crates/matrix-sdk-sled/Cargo.toml +++ b/crates/matrix-sdk-sled/Cargo.toml @@ -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", diff --git a/crates/matrix-sdk-sled/src/lib.rs b/crates/matrix-sdk-sled/src/lib.rs index e7c8eface..c3b57d15f 100644 --- a/crates/matrix-sdk-sled/src/lib.rs +++ b/crates/matrix-sdk-sled/src/lib.rs @@ -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, + passphrase: Option<&str>, +) -> Result { + 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) +}