diff --git a/crates/matrix-sdk-sled/src/lib.rs b/crates/matrix-sdk-sled/src/lib.rs index 2ac1a513c..b29af173a 100644 --- a/crates/matrix-sdk-sled/src/lib.rs +++ b/crates/matrix-sdk-sled/src/lib.rs @@ -1,5 +1,5 @@ #[cfg(feature = "state-store")] -use matrix_sdk_base::store::StoreError; +use matrix_sdk_base::store::{StoreError, StoreConfig}; #[cfg(feature = "crypto-store")] use matrix_sdk_crypto::store::CryptoStoreError; use sled::Error as SledError; @@ -34,3 +34,59 @@ pub enum OpenStoreError { #[error(transparent)] Sled(#[from] SledError), } + + +// FIXME Move these two methods back to the matrix-sdk-sled crate once weak +// dependency features are stable and we decide to bump the MSRV. + +/// 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. +/// +/// [`StoreConfig`]: #StoreConfig +#[cfg(any(feature = "state-store", feature = "crypto-store"))] +pub fn make_store_config( + path: impl AsRef, + passphrase: Option<&str>, +) -> Result { + #[cfg(all(feature = "crypto-store", feature = "state-store"))] + { + let (state_store, crypto_store) = open_stores_with_path(path, passphrase)?; + Ok(StoreConfig::new().state_store(state_store).crypto_store(crypto_store)) + } + + #[cfg(all(feature = "crypto-store", not(feature = "state-store")))] + { + let crypto_store = CryptoStore::open_with_passphrase(path, passphrase)?; + Ok(StoreConfig::new().crypto_store(Box::new(crypto_store))) + } + + #[cfg(not(feature = "crypto-store"))] + { + let state_store = if let Some(passphrase) = passphrase { + StateStore::open_with_passphrase(path, passphrase)? + } else { + StateStore::open_with_path(path)? + }; + + Ok(StoreConfig::new().state_store(Box::new(state_store))) + } +} + +/// Create a [`StateStore`] and a [`CryptoStore`] that use the same database and +/// passphrase. +#[cfg(all(feature = "state-store", feature = "crypto-store"))] +fn open_stores_with_path( + path: impl AsRef, + passphrase: Option<&str>, +) -> Result<(Box, Box), OpenStoreError> { + if let Some(passphrase) = passphrase { + let state_store = StateStore::open_with_passphrase(path, passphrase)?; + let crypto_store = state_store.open_crypto_store(Some(passphrase))?; + Ok((Box::new(state_store), Box::new(crypto_store))) + } else { + let state_store = StateStore::open_with_path(path)?; + let crypto_store = state_store.open_crypto_store(None)?; + Ok((Box::new(state_store), Box::new(crypto_store))) + } +} diff --git a/crates/matrix-sdk/Cargo.toml b/crates/matrix-sdk/Cargo.toml index bc8196fd8..beb62c1e8 100644 --- a/crates/matrix-sdk/Cargo.toml +++ b/crates/matrix-sdk/Cargo.toml @@ -160,4 +160,4 @@ required-features = ["encryption"] [[example]] name = "timeline" -required-features = ["sled-state-store"] +required-features = ["sled"] diff --git a/crates/matrix-sdk/examples/autojoin.rs b/crates/matrix-sdk/examples/autojoin.rs index 068088e2c..158e82aeb 100644 --- a/crates/matrix-sdk/examples/autojoin.rs +++ b/crates/matrix-sdk/examples/autojoin.rs @@ -44,7 +44,7 @@ async fn login_and_sync( #[allow(unused_mut)] let mut client_builder = Client::builder().homeserver_url(homeserver_url); - #[cfg(feature = "sled-state-store")] + #[cfg(feature = "sled")] { // The location to save files to let mut home = dirs::home_dir().expect("no home directory found"); @@ -53,7 +53,7 @@ async fn login_and_sync( client_builder = client_builder.state_store(Box::new(state_store)); } - #[cfg(feature = "indexeddb-state-store")] + #[cfg(feature = "indexeddb")] { let state_store = matrix_sdk_indexeddb::StateStore::open(); client_builder = client_builder.state_store(Box::new(state_store)); diff --git a/crates/matrix-sdk/examples/command_bot.rs b/crates/matrix-sdk/examples/command_bot.rs index e9b4dfe6b..4620a1cab 100644 --- a/crates/matrix-sdk/examples/command_bot.rs +++ b/crates/matrix-sdk/examples/command_bot.rs @@ -39,7 +39,7 @@ async fn login_and_sync( #[allow(unused_mut)] let mut client_builder = Client::builder().homeserver_url(homeserver_url); - #[cfg(feature = "sled-state-store")] + #[cfg(feature = "sled")] { // The location to save files to let mut home = dirs::home_dir().expect("no home directory found"); @@ -48,7 +48,7 @@ async fn login_and_sync( client_builder = client_builder.state_store(Box::new(state_store)); } - #[cfg(feature = "indexeddb-state-store")] + #[cfg(feature = "indexeddb")] { let state_store = matrix_sdk_indexeddb::StateStore::open(); client_builder = client_builder.state_store(Box::new(state_store)); diff --git a/crates/matrix-sdk/src/store.rs b/crates/matrix-sdk/src/store.rs index c2e4b4171..89c7d1cde 100644 --- a/crates/matrix-sdk/src/store.rs +++ b/crates/matrix-sdk/src/store.rs @@ -29,62 +29,7 @@ //! [`StoreConfig`]: crate::config::StoreConfig //! [`ClientBuilder::store_config()`]: crate::ClientBuilder::store_config -#[cfg(any(feature = "indexeddb-state-store", feature = "indexeddb-crypto-store"))] +#[cfg(feature = "indexeddb")] pub use matrix_sdk_indexeddb::*; -#[cfg(any(feature = "sled-state-store", feature = "sled-crypto-store"))] -pub use matrix_sdk_sled::*; - -// FIXME Move these two methods back to the matrix-sdk-sled crate once weak -// dependency features are stable and we decide to bump the MSRV. - -/// 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. -/// -/// [`StoreConfig`]: #crate::config::StoreConfig -#[cfg(any(feature = "sled-state-store", feature = "sled-crypto-store"))] -pub fn make_store_config( - path: impl AsRef, - passphrase: Option<&str>, -) -> Result { - #[cfg(all(feature = "e2e-encryption", feature = "sled-state-store"))] - { - let (state_store, crypto_store) = open_stores_with_path(path, passphrase)?; - Ok(crate::config::StoreConfig::new().state_store(state_store).crypto_store(crypto_store)) - } - - #[cfg(all(feature = "e2e-encryption", not(feature = "sled-state-store")))] - { - let crypto_store = CryptoStore::open_with_passphrase(path, passphrase)?; - Ok(crate::config::StoreConfig::new().crypto_store(Box::new(crypto_store))) - } - - #[cfg(not(feature = "e2e-encryption"))] - { - let state_store = if let Some(passphrase) = passphrase { - StateStore::open_with_passphrase(path, passphrase)? - } else { - StateStore::open_with_path(path)? - }; - - Ok(crate::config::StoreConfig::new().state_store(Box::new(state_store))) - } -} - -/// Create a [`StateStore`] and a [`CryptoStore`] that use the same database and -/// passphrase. -#[cfg(all(feature = "sled-state-store", feature = "sled-crypto-store"))] -fn open_stores_with_path( - path: impl AsRef, - passphrase: Option<&str>, -) -> Result<(Box, Box), OpenStoreError> { - if let Some(passphrase) = passphrase { - let state_store = StateStore::open_with_passphrase(path, passphrase)?; - let crypto_store = state_store.open_crypto_store(Some(passphrase))?; - Ok((Box::new(state_store), Box::new(crypto_store))) - } else { - let state_store = StateStore::open_with_path(path)?; - let crypto_store = state_store.open_crypto_store(None)?; - Ok((Box::new(state_store), Box::new(crypto_store))) - } -} +#[cfg(feature = "sled")] +pub use matrix_sdk_sled::*; \ No newline at end of file